move_uploaded_file() fails
Posted: Wed Jan 28, 2009 2:56 pm
My move_uploaded_file() fails (without error) only after I try resizing an uploaded image using imagecreatefrompng(), imagecreatetruecolor(), and imagecopyresampled() first. No image is uploaded to the directory. It DOES work however, if I specify the input file as the original $_FILES['userfile']['tmp_name'] - and not the file after it goes through all those 3 functions to reduce it's size. Thanks in advance for any advice. The relevant code (the directory path is replaced in this example, but the actual path does work)is:
$extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed)
//List of allowed extensions if extlimit = yes
$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp",".tiff");
//the image -> variables
$file_type = $_FILES['userfile']['type'];
$file_name = $_FILES['userfile']['name'];
$file_size = $_FILES['userfile']['size'];
$file_tmp = $_FILES['userfile']['tmp_name'];
$img = $file_name;//setting session variable for the customer's original image file name. to be updated
//check if you have selected a file. may not want this because customer does not have to upload a photo. may put all this image processing code after test for is_puloaded_file
if(!is_uploaded_file($file_tmp)){
echo "Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit(); //exit the script and don't process the rest of it!
}
//check the file's extension
$ext = strrchr($file_name,'.');
$ext = strtolower($ext);
//uh-oh! the file extension is not allowed!
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
echo "Wrong file extension. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit();
}
/*************Resize image if too big*******************************/
$imageproperties=getimagesize($file_tmp);//for resizing image
$mimetype=image_type_to_mime_type($imageproperties[2]);//for resizing image
/*create image*/
switch($imageproperties[2]){
case IMAGETYPE_JPEG:
$file_tmp=imagecreatefromjpeg($file_tmp);
break;
case IMAGETYPE_GIF:
$file_tmp=imagecreatefromgif($file_tmp);
break;
case IMAGETYPE_PNG:
$file_tmp=imagecreatefrompng($file_tmp);
break;
default:
die("Couldn't create image.");
}
/*create reduced size*/
$reducedSize=100;//width dimension in pixels to reduce to. (could also restrict height if wanted)
$srcW = $imageproperties[0];//original image width
$srcH = $imageproperties[1];//original image height
//only reduce if orig image is larger than max
if($srcW > $reducedSize){
$reduction=round($srcW/$reducedSize);
$desW = $srcW/$reduction;
$desH = $srcH/$reduction;
$copy = imagecreatetruecolor($desW, $desH);
imagecopyresampled($copy,$file_tmp,0,0,0,0,$desW, $desH, $srcW, $srcH) or die ("Image copy failed.");
//destroy original
imagedestroy($file_tmp);
$new_file = $copy;
}
/*ok copy the finished file to the designated directory */
move_uploaded_file ($new_file, "path to directory");
$extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed)
//List of allowed extensions if extlimit = yes
$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp",".tiff");
//the image -> variables
$file_type = $_FILES['userfile']['type'];
$file_name = $_FILES['userfile']['name'];
$file_size = $_FILES['userfile']['size'];
$file_tmp = $_FILES['userfile']['tmp_name'];
$img = $file_name;//setting session variable for the customer's original image file name. to be updated
//check if you have selected a file. may not want this because customer does not have to upload a photo. may put all this image processing code after test for is_puloaded_file
if(!is_uploaded_file($file_tmp)){
echo "Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit(); //exit the script and don't process the rest of it!
}
//check the file's extension
$ext = strrchr($file_name,'.');
$ext = strtolower($ext);
//uh-oh! the file extension is not allowed!
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
echo "Wrong file extension. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit();
}
/*************Resize image if too big*******************************/
$imageproperties=getimagesize($file_tmp);//for resizing image
$mimetype=image_type_to_mime_type($imageproperties[2]);//for resizing image
/*create image*/
switch($imageproperties[2]){
case IMAGETYPE_JPEG:
$file_tmp=imagecreatefromjpeg($file_tmp);
break;
case IMAGETYPE_GIF:
$file_tmp=imagecreatefromgif($file_tmp);
break;
case IMAGETYPE_PNG:
$file_tmp=imagecreatefrompng($file_tmp);
break;
default:
die("Couldn't create image.");
}
/*create reduced size*/
$reducedSize=100;//width dimension in pixels to reduce to. (could also restrict height if wanted)
$srcW = $imageproperties[0];//original image width
$srcH = $imageproperties[1];//original image height
//only reduce if orig image is larger than max
if($srcW > $reducedSize){
$reduction=round($srcW/$reducedSize);
$desW = $srcW/$reduction;
$desH = $srcH/$reduction;
$copy = imagecreatetruecolor($desW, $desH);
imagecopyresampled($copy,$file_tmp,0,0,0,0,$desW, $desH, $srcW, $srcH) or die ("Image copy failed.");
//destroy original
imagedestroy($file_tmp);
$new_file = $copy;
}
/*ok copy the finished file to the designated directory */
move_uploaded_file ($new_file, "path to directory");