I have an update controller function in a laravel resource controller thats pretty basic. Updates the title, body etc. in the DB as normal (no issues with data updates).
The issue is with images. My image directory for each project follows a slug like naming convention based on the title of the project. I built the update function so that the directory and images would be renamed if the title was changed. This was working fine as soon as I built it, but it's suddenly stopped. The directory renames, but the files do not. Here is the relevant snippet from the controller:
$count = 0;
$slug = Input::get('title');
$prefix = Str::slug($slug);
$oldPrefix = $project->imagesprefix;
$path = public_path().'/img/projects/';
$directory = $path . $prefix;
rename($path.$oldPrefix, $directory);
//dumped directory here and it's definitely set
$files = preg_grep('~\.(jpg)$~', scandir($directory));
//dumped $files here and the array is definitely set
foreach($files as $file){
rename($directory.'/'.$file, $directory.'/'.$prefix.'-'.$count.'.jpg');
++$count;
}
rename($directory.'/thumbnails/'.$oldprefix.'-thumb.jpg', $directory.'/thumbnails/'.prefix.'-thumb.jpg');
All of this was working fine when tested this afternoon. Now it updates the DB, renames the directory folder but fails to rename the files. I've dumped the both $files and $directory before the loop and they are both set.
If file rename fails then this is quite often related to permissions.