Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save abdessamadely/d042b1541878ebd1d65a3f8e14ab8fa4 to your computer and use it in GitHub Desktop.
Save abdessamadely/d042b1541878ebd1d65a3f8e14ab8fa4 to your computer and use it in GitHub Desktop.
Common functions, for automation with PHP and Laravel
/**
* @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