Help with Upload Script

Discussion in 'OT Technology' started by AndyB, Dec 2, 2003.

  1. AndyB

    AndyB I love you baby Kaitlyn and Dad

    Joined:
    Dec 26, 2001
    Messages:
    93,194
    Likes Received:
    119
    Location:
    Auburn, Mexifornia
    First time working with anytype of scripts and having trouble getting an upload script to work.

    Here is the form I have here: http://andyb.stlhosting.com/Upload/
    Code:
    <HTML> 
     <HEAD></HEAD> 
     <BODY> 
     <FORM ACTION="upload.cgi" METHOD="post" ENCTYPE="multipart/form-data"> 
     Photo to Upload: <INPUT TYPE="file" NAME="photo"> 
     <br><br> 
     Your Email Address: <INPUT TYPE="text" NAME="email_address"> 
     <br><br> 
     <INPUT TYPE="submit" NAME="Submit" VALUE="Submit Form"> 
     </FORM> 
     </BODY> 
    </HTML>
    
    and here is the script I got off another website that I am trying to adapt.

    Code:
    
    #!/usr/bin/perl -w 
    
     use CGI; 
    
     $upload_dir = "/Upload"; 
    
     $query = new CGI; 
    
     $filename = $query->param("photo"); 
     $email_address = $query->param("email_address"); 
     $filename =~ s/.*[\/\\](.*)/$1/; 
     $upload_filehandle = $query->upload("photo"); 
    
     open UPLOADFILE, ">$upload_dir/$filename"; 
    
     while ( <$upload_filehandle> ) 
     { 
       print UPLOADFILE; 
     } 
    
     close UPLOADFILE; 
    
     print $query->header ( ); 
     print <<END_HTML; 
    
     <HTML> 
     <HEAD> 
     <TITLE>Thanks!</TITLE> 
     </HEAD> 
    
     <BODY> 
    
     <P>Thanks for uploading your photo!</P> 
     <P>Your email address: $email_address</P> 
     <P>Your photo:</P> 
     <img src="/upload/$filename" border="0"> 
    
     </BODY> 
     </HTML> 
    
     END_HTML
    
    

    Anyone got any ideas what I am doing wrong? Thanks :wiggle:
     
  2. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Hm the HTML part looks right, so it must be the Perl part. It's a lot easier in PHP, does your server have PHP installed?
     
  3. AndyB

    AndyB I love you baby Kaitlyn and Dad

    Joined:
    Dec 26, 2001
    Messages:
    93,194
    Likes Received:
    119
    Location:
    Auburn, Mexifornia
    Yup looks like it
    From my control panel info
     
  4. AndyB

    AndyB I love you baby Kaitlyn and Dad

    Joined:
    Dec 26, 2001
    Messages:
    93,194
    Likes Received:
    119
    Location:
    Auburn, Mexifornia
    umm I mean

    PHP version 4.3.3
    :o
     
  5. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    OK, then do this:

    Code:
    // The directory that the images will be uploaded to
    $imagedir = "/path/to/image/dir";
    
    // For the names of photos we already have
    $exists = '';
    
    // Get the information about the file
    $filename = $imagedir . $_FILES['photo']['name'];
    $picname = $_FILES['photo']['name'];
    
    // Make sure we're not overwriting anything
    if(!file_exists($filename)) {
    	// Move each file
    	if(move_uploaded_file($_FILES['photo']['tmp_name'], $filename)) {
    		// Chmod it!
    		chmod($filename, 0644);
    	}
    // So we can tell the user to rename things.
    } else if($_FILES['photo']['name']) {
    	$exists = $_FILES['photo']['name'] . ", ";
    }
    
    if($exists) {
    	echo("The photo ($exists) could not be uploaded as they already exist on the server.  Please rename these files and try again.");
    }
    Might have to play with it a little to get the field names and such correct, but it should work (it's from a site I did, so I know it does). If you need help with it, post up here or look at php.net.

    Edit: Remembered that the script was written for an array of file fields, fixed to just do one.
     
  6. Buzz Killington

    Buzz Killington nunc fortunatus sum

    Joined:
    May 6, 2001
    Messages:
    147,895
    Likes Received:
    211
    Location:
    The Residency, Lucknow
    Just use the celeron dude uploader. I am an idiot and made it work in 10 minutes.
     
  7. druid

    druid What do the 5 fingers say?

    Joined:
    May 13, 2001
    Messages:
    702
    Likes Received:
    0
    You should check file types as well, you only want them to be able to upload an image. If you allow them to upload anything some little script kiddie is gonna upload a script and screw your entire site.

     
  8. AndyB

    AndyB I love you baby Kaitlyn and Dad

    Joined:
    Dec 26, 2001
    Messages:
    93,194
    Likes Received:
    119
    Location:
    Auburn, Mexifornia

    :eek3: Awesome :bowdown: Took me like 10 minutes too :o
     
  9. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    Good idea. When I wrote it, it wasn't for public use so it didn't matter, but if there are regular people uploading with it you should check file types.
     

Share This Page