In my upload photo script, at the processing part, when it moves the upload file to the uploaded_files:
@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
or error('receiving directory insuffiecient permission', $uploadForm);
Before this I added a function:
makeThumbnail($_FILES[$fieldname]['type'], $_FILES[$fieldname]['name'], $_FILES[$fieldname]['size'], $_FILES[$fieldname]['tmp_name'], 100);
that makes it a thumbnail. I wish to have the original image and a thumbnail of it, but somehow it only works if I either remove the makeThumbnail() or move_uploaded_file(), and then I can’t get both the image and the thumbnail…
I think it’s because the tmp_name gets removed after one of these have been executed, so what do I do to keep it for the both?
My thumb function:
function makeThumbnail($type, $name, $size, $tmp_name, $thumbSize) {
$path_thumbs = "uploaded_files/";
$img_thumb_width = $thumbSize; //
$extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed)
//List of allowed extensions if extlimit = yes
$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");
//the image -> variables
$file_type = $type;
$file_name = $name;
$file_size = $size;
$file_tmp = $tmp_name;
//check if you have selected a 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();
}
//so, whats the file's extension?
$getExt = explode ('.', $file_name);
$file_ext = $getExt[count($getExt)-1];
//create a random file name
$rand_name = md5(time());
$rand_name= rand(0,999999999);
//the new width variable
$ThumbWidth = $img_thumb_width;
/////////////////////////////////
// CREATE THE THUMBNAIL //
////////////////////////////////
//keep image type
if($file_size){
if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
$new_img = imagecreatefromjpeg($file_tmp);
}elseif($file_type == "image/x-png" || $file_type == "image/png"){
$new_img = imagecreatefrompng($file_tmp);
}elseif($file_type == "image/gif"){
$new_img = imagecreatefromgif($file_tmp);
}
//list the width and height and keep the height ratio.
list($width, $height) = getimagesize($file_tmp);
//calculate the image ratio
$imgratio=$width/$height;
if ($imgratio>1){
$newwidth = $ThumbWidth;
$newheight = $ThumbWidth/$imgratio;
}else{
$newheight = $ThumbWidth;
$newwidth = $ThumbWidth*$imgratio;
}
//function for resize image.
if (function_exists(imagecreatetruecolor)){
$resized_img = imagecreatetruecolor($newwidth,$newheight);
}else{
die("Error: Please make sure you have GD library ver 2+");
}
//the resizing is going on here!
imagecopyresampled($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//finally, save the image
ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext", 100);
ImageDestroy ($resized_img);
ImageDestroy ($new_img);
}
//ok copy the finished file to the thumbnail directory
// move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");
exit();
}
Unless you’ve defined the
makeThumbnail()function take parameters passed by reference, there’s no way anything inside that function could remove the original data in the $_FILES array.However, if you are running the
move_uploaded_files()before you build the thumbnail, then there’s your problem. It literally moves the file from its temporary storage location, as specified in$_FILES[...]['tmp_name']to the destination filename. Then there’ll be nothing left for your thumbnail generator to work on.Make it generate the thumbnail first and things should work better.
As a security tip, don’t trust the
['type']parameter in the $_FILES array. That’s set by the client, and can be forged. Best to determine what kind of image you’ve got withgetimagesize()orfileinfo(), which work purely server-side.followup:
Man that’s some ugly code. Some problems:
['size']parameter isn’t enough. A partial upload will still have a['size']but the file will be truncated and corrupted.So, to fix this, change your function call to this:
There’s no reason to pass in all those parameters manually when they’re already nicely grouped in an array for you. Now that we have all the available data passed in, we can start doing stuff:
This is, of course, untested, just going from your original code and off the top of my head, but it should get you started on road to something that works better.