Created
January 6, 2020 18:51
-
-
Save abdessamadely/d042b1541878ebd1d65a3f8e14ab8fa4 to your computer and use it in GitHub Desktop.
Common functions, for automation with PHP and Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param $file_path String | |
* | |
* Function to replace all spaces from the files name inside a directory | |
**/ | |
use Illuminate\Support\Facades\File; | |
public function addHyphensToFiles($files_path) | |
{ | |
$files = File::files(public_path('/directory/path')); | |
foreach($files as $file) | |
{ | |
$oldname = $file->getRealPath(); | |
$newname = explode('\\', $oldname); | |
array_pop($newname); | |
$newname = implode('\\', $newname); | |
$ext = '.' . $file->getExtension(); | |
$filename = $file->getFilename(); | |
$filename = preg_replace('/\s+/', '-', $filename); | |
$newname .= '\\' . $filename; | |
rename($oldname, $newname); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment