Basic info:
So, someone messed up and broke an image-uploading function. The extension .
was taken out of the occasion... This means all images got saved like imagejpg
instead of image.jpeg
.
I figured; No problem, I'll just loop over the folders and rename the files, right? easy peasy;
public function renameImgs(){
$dirs = array_filter(glob(DOCROOT.'/uploads/products/*'), 'is_dir');
foreach($dirs as $dir){
$files = glob($dir.'/*');
foreach($files as $file){
$filename = strtolower($file);
$newFileName = str_replace('jpg', '.jpg', $filename);
if(!copy($filename, $newFileName)){
echo "failed to copy file: $filename";
}
else{
unlink($filename);
}
//if(imagecreatefromjpeg($filename)){
//echo "Can still create image from $filename";
//}
}
}
echo "done";
}
I have also tried the rename()
function instead of copy()
, but that apparently doesn't really matter.
Anyway.
The problem
When I do this, all images get corrupted. If I try to open them on my PC after downloading, the image will flash by before telling me it's corrupt. A few came through, though they all have visual corruption.
When I take the backed files (luckily I did back it up) and rename them manually on my PC (windows), the images are perfectly fine. However, we're talking about 1800 images here. I really don't feel like doing this manually when I should be able to just fix it with a script.
The solution (I think)
I feel like I could just rename the file without it being validated in one way or another, it should work?
The imagecreatefromjpeg
function didn't come through even once, FYI.
TL;DR
I want to fix the image extensions without breaking them