On 3 Aug 2004, at 9:25 AM, Alex Brelsfoard wrote:

hi

( 04.08.02 19:32 -0400 ) Alex Brelsfoard:
But I am a bit curious to know if there is a way to do this without
using CGI.pm.

sure, mod_perl [much faster, lighter]. the interface is a bit better
than CGI.pm, but not a whole lot. i just think it's a tricky thing to be
doing anyway [uploading files].



Perhaps I should start looking into using mod_perl then. Because even
with Ron and Sean's suggestions, I am still not able to get this function
to work. (sorry guys, thanks though).


#I list these two lines up at the top of my file.
use CGI qw/:standard/;
$query = new CGI;
# Here is my function:
sub UploadPicture {

        my $photoID = $_[0]; # ID # for photo
        my $file = $_[1]; # field name from <form>
        my $picLoc = $_[2]; #any extra directory needed.
        my $filepath = "$UPLOAD_DIR/$picLoc$photoID$file\.jpg";

        # Already did "" statement outside this function.
        my $picture = $query->upload("$file");

        open (UPLOAD, ">$filepath") || &Error("Could not write: $filepath");
        while ($bytesread=read($picture,$buffer,1024))
        { print UPLOAD $buffer; }
        close (UPLOAD);
}


Here is some code I've written that does work... It's using CGI.pm. What follows is short snippets of the code, not the whole thing...


at the start of the file:

#!perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw/fatalsToBrowser/;

my $q = new CGI;

later on, I create the form:

<form method="post" action="/path/to/script" enctype="multipart/form-data">
File to upload: <input type="file" size="40" name="data"><br />
<input type="submit" value="Upload file">
</form>



then, I use the following code to retrieve the file:

# first, we need to grab the uploaded file's file handle, and check
# to see if there was an error during the send....
my $fh = $q->upload('data');
if (!$fh and $q->cgi_error) {
        die("File upload error: " . $q->cgi_error);
} elsif (!$fh) {
        die("No file specified");
}

my $fname = "output_file";
# at this point, we have a good file that has been uploaded....
open(my $out, ">", "$WORKING_DIR/$fname") or die("Could not create file: $!");


# we make sure that we have raw character discipline
binmode($fh, ":raw");
binmode($out, ":raw");

# now, read the data using buffered IO
my $bytesread;
my $buffer;
while($bytesread = read($fh, $buffer, 1024)) {
        print $out $buffer;
}

# read returns undef on an error. I don't know where this error gets stored
# but I'm guessing $!
if (!defined($bytesread)) {
die("An error occured while trying to write the file: $!");
}


# now close the files...
close($fh) or die("couldn't close uploaded file: $!");
close($out) or die("couldn't close copied file: $!");


you can obviously modify this code as needed, but it looks fairly close to what you need.


Hopefully, people won't find that I've made too many ugly mistakes, or forgotten to check some important return codes (although if I have, please let me know...)

HTH,
Ricky

_______________________________________________
Boston-pm mailing list
[EMAIL PROTECTED]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to