in reply to Uploading Files

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;

Replies are listed 'Best First'.
Re: Re: Uploading Files
by Kiko (Scribe) on Jul 18, 2001 at 22:35 UTC
    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