www.webdeveloper.com
Results 1 to 6 of 6

Thread: File Upload through Form

Hybrid View

  1. #1
    Join Date
    Nov 2005
    Posts
    2

    Post File Upload through Form

    I am working on a form that collects user input and emails though a simple HTML form. I want to be able to allow the user to upload a file to be sent along with the rest of the form information. Everything works except the file side. Any suggestions on how to code the file upload?

    This is what I've got, but I know something is missing. Just not sure what.

    Upload a JPG or GIF file from your computer:<br></b></font><input type=hidden name=max_file_size value=5000000> <input type=file size=40 name=userfile>

  2. #2
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    Refer to the CGI module's documentation for accessing the uploaded file within the script and the MIME::Lite module for attaching the file to an email.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  3. #3
    Join Date
    Dec 2002
    Location
    Pleasanton, CA
    Posts
    2,132
    My guess...
    You forgot to include...

    enctype="multipart/form-data"

    in your <form> tag

  4. #4
    Join Date
    Nov 2005
    Posts
    2
    Thanks for the help so far. I did forget that tag. I still can't quite figure this out though. I can see the name of the file, but it isn't attaching. Here's the form:
    <FORM ACTION="/bin/script_library/form_handler_mail" METHOD=post enctype="multipart/form-data" >
    What's your name?
    <INPUT TYPE="text" NAME="name"><BR>
    What's yout email address?
    <INPUT TYPE="text" NAME="email">
    <BR>
    <input type=hidden name="state" value="changePic"><input type=hidden name="rand" value="84"><tr valign=middle><td align=center><font size=2><b>Upload a JPG or GIF file from your computer:<br></b></font><input type=hidden name=max_file_size value=5000000> <input type=file size=40 name=userfile><br><input type=submit name=uploadFile value="Upload File">

    And here is what I get as the result:
    The following data was submitted via your form:

    email: gfhfgh
    max_file_size: 5000000
    name: gfhgfh
    rand: 84
    state: changePic
    uploadFile: Upload File
    userfile: C:\Documents and Settings\Owner\Desktop\favicon.ico

    No actual attatchment. I glanced throught the MIME info, but will admit it is over my head.

  5. #5
    Join Date
    Dec 2005
    Posts
    8
    you've got the actual data, but you need to open the file in your cgi/php and read it, like so:
    #!/opt/perl/bin/perl
    use CGI qw/:standard/;
    # DO NOT REMOVE. You must have CGI.PM module on your server. (MOST SERVERS DO.)

    use CGI::Carp qw/fatalsToBrowser/;
    # Carp helps with debugging REMOVE IF Carp is not on your server, It should be.

    $| = 1;

    $title='title';
    $dir='/tmp';
    #This was the relative/absolute path to the directory you wish to upload to.
    # ./ is the same directory as this script is placed. NEVER USE \
    $limit=500;
    # max xKb posts. Max file size in KB to upload.

    @extens=qw(jpg gif bmp txt html htm pdf vso ppt xls doc);
    # only upload files with the ext's in the brackets.
    # ie @extens= qw(txt html htm asp this that);

    $encoding='multipart/form-data';
    $match=0;
    $CGI::POST_MAX=1024 * $limit;
    $q = new CGI;
    print $q->header();

    print $q->start_html(-title=>"$title\n",
    -BGCOLOR=>'white',
    -TEXT=>'navy',
    -link=>'navy',
    -vlink=>'red',
    -alink=>'black');
    print "\n";
    print $q->startform($method,$action,$encoding);
    print $q->h1("$title"), "Use this page to attach a file to an ATM issue.<br>This issue must already exist on ATM!<br><br>The file will be sent to '$FTP_SITE:/ftp/$FTP_DIR'<br><br>Enter your file to upload <font size=1>(@extens ${limit}Kb Max.)</font>\n";

    ($junk, $issue_id)=split(/=/, $ENV{'QUERY_STRING'});

    print $q->filefield(-name=>'upload_file', -default=>'starting value', -size=>50, -maxlength=>180);
    print $q->submit(-name=>'button_name', -value=>'UPLOAD');
    print $q->endform;
    ...
    open(OUTFILE, ">/tmp/$file") || &error ("Can't open /tmp/$file. $!");
    binmode OUTFILE;
    # binmode is for windows only. Ignored by unix
    print "<b>Uploading document..please wait......";
    while ($bytesread=read($filename,$buffer,1024))
    {
    #print "Printing to outfile\n";
    print OUTFILE $buffer;
    }
    close (OUTFILE);
    .....
    make your mods to above.

  6. #6
    Join Date
    Dec 2002
    Location
    Pleasanton, CA
    Posts
    2,132
    <FORM ACTION="/bin/script_library/form_handler_mail" METHOD=post enctype="multipart/form-data" >

    That does not look right... It would expect something like this
    "/cgi-bin/script_library/form_handler_mail.pl" or .cgi

    But maybe it's OK. Check with your host.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles