Is it possible to retain file path for image to be uploaded?

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
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Is it possible to retain file path for image to be uploaded?

Post by edawson003 »

Is it possible to retain file path for image file that is in process of being uploaded? For instance, my form has a few conditional errors that if fired, the form will not post until the applicable conditions are me (corrected). After an unsuccessful submission attempt, the user has to re-browse for the image file again everytime. I am thinking they may be a better way for this to work. If so how can I implement?
uploadpict.jpg
uploadpict.jpg (12.89 KiB) Viewed 116 times
Here's my php code:

Code: Select all

$target = "upload_picts/"; //Specifies what directory on the web server to save image file
    $image_width = 100; 
    $image_height = 100;     
    $size = GetImageSize($_FILES['uploaded']['tmp_name']);
    
    if (($size) && ($size[0] != $image_width) || ($size) && ($size[1] != $image_height)){
    $errors[] = "Yes";
    $sizerror = "\t<span class=pred>Your pict file must be 100 by 100 pixels.<br></span>\n";
    }
        
    if($_FILES['uploaded']['size'] > 10000 && $_FILES['uploaded']['tmp_name'] != NULL)
    {
    $errors[] = "Yes";
    $fileerror = "\t<span class=pred>Your picture file size cannot be over 10k.<br></span>\n"; 
    }
    elseif($_FILES['uploaded'][type] != "image/gif" && $_FILES['uploaded'][type] != "image/pjpeg" && $_FILES['uploaded']['tmp_name'] != NULL) {
    $errors[] = "Yes";
    $filetypeerror = "\t<span class=pred>You may only upload .GIF or .JPEG files.</span>\n";    
    }   
    function findexts ($filename) 
    { 
    $filename = strtolower($filename) ; 
    $exts = split("[/\\.]", $filename) ; 
    $n = count($exts)-1; 
    $exts = $exts[$n]; 
    return $exts; 
    }
    $ext = findexts($_FILES['uploaded']['name']);  
    $namegen=substr('00000000' . rand(1, 99999999), -7);
    $newpicturename = $namegen . "." . $ext;
             $target = $target . $newpicturename; 
    $picturepath = "upload_picts/" . $newpicturename;
 
if(empty($errors)){
move_uploaded_file($_FILES['uploaded']['tmp_name'], $target);   
}
 
and my input tag code:

Code: Select all

 <table width="250" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td align=center><p>Upload picture file(must be under 10k):</p></td>
      
    </tr>
    <tr>
      <td height="20" align=right><INPUT NAME="uploaded" TYPE="file">
      </td>
      
    </tr>
    <tr height=0>
      <td align=center><? echo $fileerror; ?> <?echo $sizerror; ?> <? $filetypeerror; ?></td>
      
    </tr>    
  </table>
    
   
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Is it possible to retain file path for image to be uploaded?

Post by Mark Baker »

You'll probably need to read the full file path in javascript onSubmit and store it in a hidden text field before allowing the browser to actually submit the request to the server
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: Is it possible to retain file path for image to be uploaded?

Post by edawson003 »

Cool. Thanks for the clue. Not sure how to execute, so I'll start looking into how to pull that off.
Post Reply