coolphpdude Posted April 14, 2008 Share Posted April 14, 2008 hey there, ive almost got my uploads page working. I have one problem though and i am sure one of you lot won't have a trouble spotting where i am going wrong. I am allowing the user to upload an image. on upload the image is resized to a main picture and a thumbnail size pic. So two pictures created from one original. Now to the best of my understanding my problem lies with the move_uploaded_file($_FILES['upload']['tmp_name'], $main_target) part. I will copy in my code minus all the crap that you dont need. <?php session_start(); function resampleimage($maxsize, $sourcefile, $destination, $imgcomp=0){ // SET THE IMAGE COMPRESSION $g_imgcomp=100-$imgcomp; // CHECK TO SEE IF THE IMAGE EXISTS FIRST if(file_exists($sourcefile)){ // FIRST WE GET THE CURRENT IMAGE SIZE $g_is=getimagesize($sourcefile); /********* CALCULATE THE WIDTH AND THE HEIGHT ***************/ // CHECK TO SEE IF THE WIDTH AND HEIGHT ARE ALREADY SMALLER THAN THE MAX SIZE if($g_is[0] <= $maxsize && $g_is[1] <= $maxsize){ // LEAVE WIDTH AND HEIGHT ALONE IF IMAGE IS SMALLER THAN MAXSIZE $new_width=$g_is[0]; $new_height=$g_is[1]; } else { // GET VALUE TO CALCULATE WIDTH AND HEIGHT $w_adjust = ($maxsize / $g_is[0]); $h_adjust = ($maxsize / $g_is[1]); // CHECK TO WHICH DIMENSION REQUIRES THE SMALLER ADJUSTMENT if($w_adjust <= $h_adjust){ // CALCULATE WIDTH AND HEIGHT IF THE WIDTH VALUE IS SMALLER $new_width=($g_is[0]*$w_adjust); $new_height=($g_is[1]*$w_adjust); } else { // CALCULATE WIDTH AND HEIGHT IF THE HEIGHT VALUE IS SMALLER $new_width=($g_is[0]*$h_adjust); $new_height=($g_is[1]*$h_adjust); } } //SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER THE LAST "." ) $image_type = strrchr($sourcefile, "."); //SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION switch($image_type) { case '.jpg': $img_src = imagecreatefromjpeg($sourcefile); break; case '.jpeg': $img_src = imagecreatefromjpeg($sourcefile); break; case '.png': $img_src = imagecreatefrompng($sourcefile); break; case '.gif': $img_src = imagecreatefromgif($sourcefile); break; default: echo("Error Invalid Image Type"); die; break; } // CREATE THE TRUE COLOR IMAGE WITH NE WIDTH AND HEIGHT $img_dst=imagecreatetruecolor($new_width,$new_height); // RESAMPLE THE IMAGE TO NEW WIDTH AND HEIGHT imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $new_width, $new_height, $g_is[0], $g_is[1]); // OUTPUT THE IMAGE AS A JPEG. // THIS CAN BE CHANGED IF YOU WANT TRANSPARENCY OR PREFER ANOTHER FORMAT. MAKE SURE YOU CHANGE HEADER ABOVE. imagejpeg($img_dst, $destination, 100); // DESTROY THE NEW IMAGE imagedestroy($img_dst); return true; } else { return false; } } // *** Page Body - START *** if ((($_FILES["upload"]["type"] == "image/pjpeg") || ($_FILES["upload"]["type"] == "image/jpeg") || ($_FILES["upload"]["type"] == "image/png") || ($_FILES["upload"]["type"] == "image/bmp")) && ($_FILES["upload"]["size"] < 3000000)) { // Where the file is going to be placed $main_base = "uploads/mainpics/"; $main_target = "uploads/mainpics/"; $thumb_base = "uploads/thumb/"; $thumb_target = "uploads/thumb/"; /* Add the original filename to our target path. Result is "uploads/filename.extension" */ $main_target = $main_target . basename( $_FILES['upload']['name']); $_FILES['upload']['tmp_name']; $thumb_target = $thumb_target . basename( $_FILES['upload']['name']); $_FILES['upload']['tmp_name']; if (file_exists($main_target)) { // make the random file name $rand = md5(rand() * time()); $main_target = $main_base . $rand . basename( $_FILES['upload']['name']); if(move_uploaded_file($_FILES['upload']['tmp_name'], $main_target)) { $user="UPDATE property SET property_picture='$main_target' WHERE property_id='$temp_property_id' AND seller_id='$seller_session_id'"; // Save full size image with max width/height of 400 resampleimage("400", $main_target, $main_target, 0); // Save thumb image with max width/height of 100 resampleimage("100", $thumb_target, $thumb_target, 0); if (mysql_query($user,$db)) { echo "The file ". basename( $_FILES['upload']['name'])." has been uploaded"; } else { do_error("Error - New seller, $seller_id could not be added"); } } else { echo "There was an error uploading the file, please try again!"; } } else { //if(move_uploaded_file($_FILES['upload']['tmp_name'], $main_target) || move_uploaded_file($_FILES['upload']['tmp_name'], $thumb_target)) if(move_uploaded_file($_FILES['upload']['tmp_name'], $main_target)) { if(move_uploaded_file($_FILES['upload']['tmp_name'], $thumb_target)) { $user="UPDATE property SET property_picture='$main_target', property_thumb='$thumb_target' WHERE property_id='$temp_property_id' AND seller_id='$seller_session_id'"; // Save full size image with max width/height of 400 resampleimage("400", $main_target, $main_target, 0); // Save thumb image with max width/height of 100 resampleimage("100", $thumb_target, $thumb_target, 0); if (mysql_query($user,$db)) { echo "The file ". basename( $_FILES['upload']['name'])." has been uploaded"; } else { echo "There was an error uploading the file, please try again!"; } } else { echo "There was an error uploading the file, please try again!"; } } else { echo "There was an error uploading the file, please try again!"; } } } else { echo "Invalid file type or maximum picture file size exceeded."; } } echo "</html>"; ?> ...ignore the part where the file already exists so it creates a random filename, i will copy the part of the code from above that i am trying to make work... //if(move_uploaded_file($_FILES['upload']['tmp_name'], $main_target) || move_uploaded_file($_FILES['upload']['tmp_name'], $thumb_target)) if(move_uploaded_file($_FILES['upload']['tmp_name'], $main_target)) { if(move_uploaded_file($_FILES['upload']['tmp_name'], $thumb_target)) { $user="UPDATE property SET property_picture='$main_target', property_thumb='$thumb_target' WHERE property_id='$temp_property_id' AND seller_id='$seller_session_id'"; // Save full size image with max width/height of 400 resampleimage("400", $main_target, $main_target, 0); // Save thumb image with max width/height of 100 resampleimage("100", $thumb_target, $thumb_target, 0); if (mysql_query($user,$db)) { echo "The file ". basename( $_FILES['upload']['name'])." has been uploaded"; } else { echo "There was an error uploading the file, please try again!"; } ...i am guessing i am having problems because i have already moved the file for the main pic so it cant move it again for the thumbnail?? Any help? Quote Link to comment https://forums.phpfreaks.com/topic/101079-moving-files-uploads/ Share on other sites More sharing options...
poleposters Posted April 14, 2008 Share Posted April 14, 2008 Hi, You haven't actually stated your problem.Only told us you're having a problem. Are you getting an error message? Is the file not uploading , is a file missing??? Quote Link to comment https://forums.phpfreaks.com/topic/101079-moving-files-uploads/#findComment-516903 Share on other sites More sharing options...
coolphpdude Posted April 14, 2008 Author Share Posted April 14, 2008 Good point!!! sorry about that. My problem is that i am trying to create a main image and a thumb image resized off an original image. However, the code is only creating the resized main image, it is not creating a thumbnail, or if it is creating a thumbnail it isnt putting the image in the correct target location. does that make any sense? Quote Link to comment https://forums.phpfreaks.com/topic/101079-moving-files-uploads/#findComment-516911 Share on other sites More sharing options...
coolphpdude Posted April 14, 2008 Author Share Posted April 14, 2008 i think changing it to roughtly the following might work but it is coming back with an error on the page saying Parse error: syntax error, unexpected ','... anyway, am i writing this incorrect? if(move_uploaded_file($_FILES['upload']['tmp_name'], $main_target) && ($_FILES['upload']['tmp_name'], $thumb_target)) Quote Link to comment https://forums.phpfreaks.com/topic/101079-moving-files-uploads/#findComment-516941 Share on other sites More sharing options...
poleposters Posted April 14, 2008 Share Posted April 14, 2008 I'll have a good look at your code in the mean time. You should make sure that the directory you have specified exists. ie uploads/thumb uploads/mainpics And check out this post. It features a great upsize script. It might help point out what you're doing wrong. http://www.phpfreaks.com/forums/index.php/topic,191723.msg861082.html#msg861082 Quote Link to comment https://forums.phpfreaks.com/topic/101079-moving-files-uploads/#findComment-516948 Share on other sites More sharing options...
coolphpdude Posted April 14, 2008 Author Share Posted April 14, 2008 thanks, yeah the paths exist. i know thumbnail is working because if i change this... if(move_uploaded_file($_FILES['upload']['tmp_name'], $main_target)... TO ...if(move_uploaded_file($_FILES['upload']['tmp_name'], $thumb_target)... it creates a thumbnail in the required path. So im guessing its either the way im writing it or the fact i am trying to move a file twice ??? Quote Link to comment https://forums.phpfreaks.com/topic/101079-moving-files-uploads/#findComment-516963 Share on other sites More sharing options...
coolphpdude Posted April 14, 2008 Author Share Posted April 14, 2008 it most be the move file thing. any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/101079-moving-files-uploads/#findComment-516993 Share on other sites More sharing options...
coolphpdude Posted April 15, 2008 Author Share Posted April 15, 2008 just a bump to geth this up to the top, im still stuck. somebody must know the answer! ??? Quote Link to comment https://forums.phpfreaks.com/topic/101079-moving-files-uploads/#findComment-517489 Share on other sites More sharing options...
coolphpdude Posted April 15, 2008 Author Share Posted April 15, 2008 ok well it seems that nobody knows how to make 2 copies of one original then... so how bout copying 1 pic and resizing it to a dif location instead? Quote Link to comment https://forums.phpfreaks.com/topic/101079-moving-files-uploads/#findComment-517534 Share on other sites More sharing options...
discomatt Posted April 15, 2008 Share Posted April 15, 2008 http://php.net/manual/en/function.copy.php to copy a file Quote Link to comment https://forums.phpfreaks.com/topic/101079-moving-files-uploads/#findComment-517535 Share on other sites More sharing options...
coolphpdude Posted April 15, 2008 Author Share Posted April 15, 2008 cheers il take a luk at that now Quote Link to comment https://forums.phpfreaks.com/topic/101079-moving-files-uploads/#findComment-517539 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.