Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Uploading Files

by Kiko (Scribe)
on Jul 18, 2001 at 00:55 UTC ( [id://97448]=perlquestion: print w/replies, xml ) Need Help??

Kiko has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I'm working on a system that's supposed to take some information that a user enters and store it into a database. I have that part working. But this system should also allow the users to upload associated files with their entry. And this part i don't have working. I've never done this before and i don't even know where to start. If someone has done something similar or if you know how to go about this problem; Please Help. Thanks, Kiko

Replies are listed 'Best First'.
Re: Uploading Files
by gryphon (Abbot) on Jul 18, 2001 at 01:34 UTC

    Greetings Kiko. There are a few ways to do this. Here's my attempt to show the raw basics. (Please don't try this in an enterprise solution...)

    #!/usr/bin/perl -w use strict; use CGI; my $cgi = new CGI; my $filename = $cgi->param("formuploadinputname"); my($bytesread,$buffer,$total); open(OUTPUT, "> /var/home/me/somedirsomewhere/$filename"); binmode $filename; binmode OUTPUT; while ($bytesread = read($filename,$buffer,1024) ) { $total += $bytesread; if ($bytesread > 10000000) { # 10 meg filesize limit close(OUTPUT); unlink "/var/home/me/somedirsomewhere/$filename"; } print OUTPUT $buffer; } close(OUTPUT);

    A much better (much much better) example of how to do this sort of thing safely is Re: File Upload To Selected Directory written by Ovid. Give that node a deep read-through and you'll have a lot to play with.

    -gryphon
    code('Perl') || die;

      Hi, This is the part of my script that is supposed to upload upto 5 files. Everything else in my script works put the portion. I'm passing file1,file2,...file5 to this script. When i pass it file1 i get this error "Can't use an undefined value as a HASH reference at c:\phpdev3\scripts\mkrdb\SAVE_N~1.PL line 81" which happens to be the line where $format is. But when i pass it file2 or 3 or 4 or 5 i don't get the error and my script works but the file upload portion below.
      UPLOAD_FILE: { for my $file_num (1..5) { my $file = $grab_file->param("file$file_num") or next UPLOAD_F +ILE; if ($file) { my $buffer; my $file_handle = $grab_file->upload($file); my $format = $grab_file->uploadInfo($file)->{'Content-Type'} +; # This will create the new file sysopen OUTFILE, UPLOAD_DIR . $file, O_CREAT or die "Can't + open UPLOAD_DIR$file: $!"; while ( read( $file_handle, $buffer, BUFFER_SIZE ) ) { print OUTFILE $buffer; } close (OUTFILE); # This will store the file name in the database push @statement,"INSERT INTO documents (record_id, document) + VALUES ('$record_id', '$file')"; } } }
      Thanks for your help, Kiko
Re: Uploading Files
by thraxil (Prior) on Jul 18, 2001 at 01:04 UTC

    look at the documentation for the CGI module. it has several clear examples of how to do this.

    anders pearson

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://97448]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2024-04-23 07:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found