move_uploaded_file() fails

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JmThms
Forum Newbie
Posts: 4
Joined: Wed Jan 28, 2009 2:42 pm

move_uploaded_file() fails

Post by JmThms »

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");
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: move_uploaded_file() fails

Post by requinix »

It's called move_uploaded_file, right? It should be used to move files.

$new_file is not a filename. It is a resource describing the thumbnailed image. If you want to save that image somewhere learn more about imagepng, imagejpeg, or imagegif.
JmThms
Forum Newbie
Posts: 4
Joined: Wed Jan 28, 2009 2:42 pm

Re: move_uploaded_file() fails

Post by JmThms »

Thanks. I got the upload to work using imagepng, imagejpeg etc, and ditching the move_uploaded_file. The .jpg image uploads fine. However, the .png image uploaded images are discolored. I thought that using:

imagealphablending($copy, false);
imagesavealpha($copy, true);

just before the imagepng() might correct it, but it did not. I didn't really understand the color allocation functions and if, how and why might apply here. Any thoughts?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: move_uploaded_file() fails

Post by requinix »

Not much experience with GD and alpha transparency, but I'd imagine those functions need to be called before you do anything with the image.
Meaning right after the imagecreatetruecolor() and before any imagecopyresampling.
Post Reply