Connecting Tech Pros Worldwide Help | Site Map

cant get copy or move_uploaded_file to work

  #1  
Old June 30th, 2009, 01:08 PM
Newbie
 
Join Date: Sep 2007
Posts: 23
Im trying to get an image upload script to work within a session file.....i can get it to work on a single page without any other script....but i need to have the image upload facility built into a add product form....so the script is currently sitting in a session file at the same point as where the fields are all validatedd....i've tried using copy and move_uploaded_file.

I know the structure of the code is probably not great but i cant seem to find out why it wont work.

$subimgfile is the variable name for $_FILES['imgfile']['name']
and $subimgfiletype is the variable name for $_FILES['imgfile']['type']

Expand|Select|Wrap|Line Numbers
  1. if ($subimgfile)
  2. {
  3. $uploaddir = "./product_images";
  4. if (($subimgfiletype != "image/gif")  && ($subimgfiletype != "image/jpeg") && ($subimgfiletype != "image/pjpeg"))
  5.     {
  6.        $form->setError($field, "* Image Must be .jpg / .jpeg or .gif, your file had extension $subimgfiletype");
  7.         unlink($subimgfile);
  8.     }
  9.     $imgsize = GetImageSize($subimgfile);
  10.     if (($imgsize[0] > 531) || ($imgsize[1] > 398)) 
  11.     {
  12.         $tmpimg = tempnam("/tmp", "MKUP");
  13.         system("djpeg $subimgfile >$tmpimg");
  14.         system("pnmscale -xy 531 398 $tmpimg | cjpeg -smoo 10 -qual 80 >$subimgfile");
  15.         unlink($tmpimg);
  16.     }
  17. $final_filename = str_replace(" ", "_", $subimgfile);
  18. $FileCounter = 1; 
  19. while (file_exists( 'product_images/'.$final_filename )) 
  20.   $final_filename = $FileCounter++.$final_filename; 
  21.     $newfile = $uploaddir . "/$final_filename";
  22.     if (!copy($subimgfile,"$newfile")) 
  23.        {
  24.           $form->setError($field, "* Error uploading file");
  25.        }
  26.     unlink($subimgfile);
  27. }
  #2  
Old July 1st, 2009, 11:41 PM
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,075

re: cant get copy or move_uploaded_file to work


You need to troubleshoot your code. I don't know what those system calls do and what tempnam() does or what you're trying to do with image facilities and add product forms.

Maybe you can describe your problem in a more technical way?

Note: you do know that $_FILES['imgfile']['name'] is only the name of the file, not where it is stored on the server, right? see $_FILES['imgfile']['tmp_name']



Dan
  #3  
Old July 2nd, 2009, 10:47 AM
Newbie
 
Join Date: Sep 2007
Posts: 23

re: cant get copy or move_uploaded_file to work


Hi,

Thanks for pointing that out.....i kind of knew that but still didnt realise that was my porblem....which it was sot thanks.

The only part i cant get to work now is the resizing

Expand|Select|Wrap|Line Numbers
  1. $imgsize = GetImageSize($subimgfile);
  2.     if (($imgsize[0] > 200) || ($imgsize[1] > 200)) 
  3.     {
  4.         system("djpeg $subimgfile >$subimgfiletmp");
  5.         system("pnmscale -xy 200 200 $subimgfiletmp | cjpeg -smoo 10 -qual 80 >$subimgfile");
  6.     }
$subimgfile is the $FILES['imgfile']['name'] and
$subimgfiletmp is the $FILES['imgfile']['tmp_name']

I presume this will only work for jpegs....is there a resize script that will work for both gif and jpegs....cheers.
  #4  
Old July 2nd, 2009, 11:17 AM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

re: cant get copy or move_uploaded_file to work


Quote:
Originally Posted by ukfusion View Post
I presume this will only work for jpegs....is there a resize script that will work for both gif and jpegs....cheers.
you could try PHP’s GD library.
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
problems using copy() and rename() mantrid answers 12 October 22nd, 2006 02:25 PM