cant get copy or move_uploaded_file to work

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ukfusion
    New Member
    • Sep 2007
    • 35

    cant get copy or move_uploaded_file to work

    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_f ile.

    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']

    Code:
    if ($subimgfile)
    {
    $uploaddir = "./product_images";
    if (($subimgfiletype != "image/gif")  && ($subimgfiletype != "image/jpeg") && ($subimgfiletype != "image/pjpeg"))
        {
           $form->setError($field, "* Image Must be .jpg / .jpeg or .gif, your file had extension $subimgfiletype");
            unlink($subimgfile);
        }
        $imgsize = GetImageSize($subimgfile);
        if (($imgsize[0] > 531) || ($imgsize[1] > 398)) 
        {
            $tmpimg = tempnam("/tmp", "MKUP");
            system("djpeg $subimgfile >$tmpimg");
            system("pnmscale -xy 531 398 $tmpimg | cjpeg -smoo 10 -qual 80 >$subimgfile");
            unlink($tmpimg);
        }
    $final_filename = str_replace(" ", "_", $subimgfile);
    $FileCounter = 1; 
    while (file_exists( 'product_images/'.$final_filename )) 
      $final_filename = $FileCounter++.$final_filename; 
        $newfile = $uploaddir . "/$final_filename";
        if (!copy($subimgfile,"$newfile")) 
           {
              $form->setError($field, "* Error uploading file");
           }
        unlink($subimgfile);
    }
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    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

    Comment

    • ukfusion
      New Member
      • Sep 2007
      • 35

      #3
      Hi,

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

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

      Code:
      $imgsize = GetImageSize($subimgfile);
          if (($imgsize[0] > 200) || ($imgsize[1] > 200)) 
          {
              system("djpeg $subimgfile >$subimgfiletmp");
              system("pnmscale -xy 200 200 $subimgfiletmp | cjpeg -smoo 10 -qual 80 >$subimgfile");
          }
      $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 .

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Originally posted by ukfusion
        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.

        Comment

        Working...