Re: [Boston.pm] Re: Uploading a picture with perl
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]. -- \js oblique strategy: do nothing for as long as possible ___ Boston-pm mailing list [EMAIL PROTECTED] http://mail.pm.org/mailman/listinfo/boston-pm
Re: [Boston.pm] Re: Uploading a picture with perl
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); } I take in all the variables with the use of CGI as well, just to keep things consistant. But I still keep getting zero byte files As a side note, I do hope to elarn how to use ImageMagik at some point as well. Any advice on this would be MUCH appreciated. --Alex ___ Boston-pm mailing list [EMAIL PROTECTED] http://mail.pm.org/mailman/listinfo/boston-pm
Re: [Boston.pm] Re: Uploading a picture with perl
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=databr / 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
Re: [Boston.pm] Re: Uploading a picture with perl
# Already did statement outside this function. my $picture = $query-upload($file); Perhaps something got cut out here - where did you get the $file variable from? Did you use a CGI::filefield form to get the file name? I'm assuming you have already read this, but just in case, make sure you have done things as suggested in the Perldoc: http://search.cpan.org/~lds/CGI.pm-3.05/CGI.pm#CREATING_A_FILE_UPLOAD_FIELD Peter -- Peter R. Wood - [EMAIL PROTECTED] - http://prwdot.org/ ___ Boston-pm mailing list [EMAIL PROTECTED] http://mail.pm.org/mailman/listinfo/boston-pm
Re: [Boston.pm] Re: Uploading a picture with perl
On Mon, 2004-08-02 at 19:32, Alex Brelsfoard wrote: Ron, Sean, Thank you both. I will give it a try. Sean, no worries, I have a two-layered security system in place, and plenty of well-placed warnings. I'm perfectly happy using CGI.pm (and am currently on this project). But I am a bit curious to know if there is a way to do this without using CGI.pm. Of course you _can_ do it without CGI.pm. But why would you? It's probably one of the most broadly used modules from CPAN. Very well tested and stable. That being said, there are many things it _can_ do that I don't usually use it for. Like generating the CGI forms (I was writing HTML before Perl, so it's just easier for me to print HTML;). But for anything remotely complex (getting form args, file uploads, etc.), I'll gladly let the CGI.pm maintainers (bless their souls) keep up with the latest standards best practices. Now, I think someone mentioned mod_perl. mod_perl and CGI.pm are not, to the best of my knowledge, exclusive. All of my current projects are written as mod_perl modules (no actual directories or .cgi's needed! :), but I still use CGI.pm for some things ... laziness again I suppose. It works, I know the interface. -- Sean Quinlan [EMAIL PROTECTED] ___ Boston-pm mailing list [EMAIL PROTECTED] http://mail.pm.org/mailman/listinfo/boston-pm
Re: [Boston.pm] Re: Uploading a picture with perl
Thanks Richard, everyone. I tried your (Richard) code also. It bombs on No File Specified. Looks like somehow it can't grab ahold of the file I'm trying to upload. There must be something in the rest of my cod that is interfering with all this. But I really can't imagine what. The rest of my code is truly very simple. I decided to try once more with some of my original upload code (which DOES work on a simpler version of this app I'm creating). I know this might not be the prettiest thing around..., but here is an example of the code I'm using to take in one of the pictures: # Checking if you submitted a front side of a web-friendly picture. if ($query-param('webFront')) { #Take in the field's value. $webFrontPic = $query-param('webFront'); #Do the actual uploading... UploadPicture($photoID, $webFrontPic,'webFront'); print Selected $webFrontPic to be uploaded as the web frontbr\n; }else { Error(No Web Friendly front side picture was submitted); } Again, here is the code I am now using for the upload function: sub UploadPicture { my $photoID = $_[0]; my $file = $_[1]; my $picLoc = $_[2]; my $filepath = $UPLOAD_DIR/$picLoc$photoID$file\.jpg; $upload_filehandle = $query-upload($file); open UPLOADFILE, $filepath; while ( $upload_filehandle ) { print UPLOADFILE; } close UPLOADFILE; } ..ideas? --Alex 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=databr / 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
Re: [Boston.pm] Re: Uploading a picture with perl
Upon further testing I verified that the statement my $picture = $query-upload($file); is indeed returning undefined. What would cause that? I do know that $file DOES contain valid information (the correct name of the field holding the file to be uploaded). --Alex Thanks Richard, everyone. I tried your (Richard) code also. It bombs on No File Specified. Looks like somehow it can't grab ahold of the file I'm trying to upload. There must be something in the rest of my cod that is interfering with all this. But I really can't imagine what. The rest of my code is truly very simple. I decided to try once more with some of my original upload code (which DOES work on a simpler version of this app I'm creating). I know this might not be the prettiest thing around..., but here is an example of the code I'm using to take in one of the pictures: # Checking if you submitted a front side of a web-friendly picture. if ($query-param('webFront')) { #Take in the field's value. $webFrontPic = $query-param('webFront'); #Do the actual uploading... UploadPicture($photoID, $webFrontPic,'webFront'); print Selected $webFrontPic to be uploaded as the web frontbr\n; }else { Error(No Web Friendly front side picture was submitted); } Again, here is the code I am now using for the upload function: sub UploadPicture { my $photoID = $_[0]; my $file = $_[1]; my $picLoc = $_[2]; my $filepath = $UPLOAD_DIR/$picLoc$photoID$file\.jpg; $upload_filehandle = $query-upload($file); open UPLOADFILE, $filepath; while ( $upload_filehandle ) { print UPLOADFILE; } close UPLOADFILE; } ..ideas? --Alex 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=databr / 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 ___ Boston-pm mailing list [EMAIL PROTECTED] http://mail.pm.org/mailman/listinfo/boston-pm
Re: [Boston.pm] Dan and the Pie
At 11:00 AM -0400 8/3/04, Uri Guttman wrote: DS == Dan Sugalski [EMAIL PROTECTED] writes: DS I *did*. You put a lot of whipped cream on those things, and it DS doesn't take much to coat a contact lens. :-P DS I had a professional to consult beforehand. I really shoulda asked DS what the right way to do this sort of thing is. who? a professional pie receiver? :) Oddly enough, yes. One of the folks at the conference worked at the clown college in New York. (You meet the oddest people at these things... :) -- Dan --it's like this--- Dan Sugalski even samurai [EMAIL PROTECTED] have teddy bears and even teddy bears get drunk ___ Boston-pm mailing list [EMAIL PROTECTED] http://mail.pm.org/mailman/listinfo/boston-pm
Re: [Boston.pm] Re: Uploading a picture with perl
Upon further testing I verified that the statement my $picture = $query-upload($file); is indeed returning undefined. What would cause that? Show us the entire HTML FORM that contains the file upload field. ___ Boston-pm mailing list [EMAIL PROTECTED] http://mail.pm.org/mailman/listinfo/boston-pm
Re: [Boston.pm] Re: Uploading a picture with perl
I'm not sure why your code isn't working. It's a bit hard to debug, because we're just seeing bits and pieces. Anyway, here's a simple example that does work: #!/usr/local/bin/perl -w use strict; use CGI; my $cgi = new CGI; print $cgi-header(); print EndOfHTML; html headtitleFile Upload Example/title/head body EndOfHTML if (my $filename = $cgi-param('uploaded_file')) { my $size = -s $filename; my $buffer; while (read($filename, $buffer, 1024)) { # do something with $buffer } print pUploaded file , $cgi-escapeHTML($filename), is $size bytes./p\n; } print $cgi-start_multipart_form(), \n, $cgi-filefield(-name = 'uploaded_file', -size = 50), br\n, $cgi-submit(-name = 'submit', -value = 'Go!'), \n, $cgi-end_form(), \n; print EndOfHTML; /body /html EndOfHTML __END__ HTH, Ronald ___ Boston-pm mailing list [EMAIL PROTECTED] http://mail.pm.org/mailman/listinfo/boston-pm
Re: [Boston.pm] Re: Uploading a picture with perl
I am not at all certain of what I did differently. I was playing around with all of your suggestions. And one of my attempts ACTUALLY WORKED! So thank you all very much for your help. I have tested it several times now. If you are still curious and want to see what I've got (code), I'll be happy to post it. --Alex ___ Boston-pm mailing list [EMAIL PROTECTED] http://mail.pm.org/mailman/listinfo/boston-pm
Re: [Boston.pm] Dan and the Pie
On Tuesday, Aug 3rd 2004 at 08:39 -0400, quoth Dan Sugalski: =At 12:08 AM -0400 8/3/04, Uri Guttman wrote: = DS == Dan Sugalski [EMAIL PROTECTED] writes: = =and yours truly purchased the pies and the beer (with funds from the = tpf =auction which had a group bid of $520 for us to also pie dan). they =were coconut cream and chocolate cream and i sprayed a whole can of = real =whipped cream on top of them! :) = = DS Ah, so *that's* why they gunked up my contacts so bad. I'd wondered. = = and you should have closed your eyes, dummy! :) = =I *did*. You put a lot of whipped cream on those things, and it doesn't take =much to coat a contact lens. :-P = =I had a professional to consult beforehand. I really shoulda asked what the =right way to do this sort of thing is. Oddly enough, it turns out that there are two kinds of pie: Eating and Throwing. The technology was first developed by the philosopher Milton who later in life was known more popularly as Uncle Miltie. He discovered that the best Throwing Pies are actually filled with shaving cream. Different types of shaving cream have different consistencies and can be modified for different effects. Of course, if you want a Throwing Pie that allows the receiver to comically taste what's dripping off his face, then you want to go with the Eating Pie. After that, it's all in the name. Of course we all know that a kumquat pie is funnier than a blueberry pie. Probably the funniest of all pies is the kiwi pie. -- Time flies like the wind. Fruit flies like a banana. Stranger things have .0. happened but none stranger than this. Does your driver's license say Organ ..0 Donor?Black holes are where God divided by zero. Listen to me! We are all- 000 individuals! What if this weren't a hypothetical question? steveo at syslang.net ___ Boston-pm mailing list [EMAIL PROTECTED] http://mail.pm.org/mailman/listinfo/boston-pm
Re: [Boston.pm] Dan and the Pie
On Tue, 3 Aug 2004, Steven W. Orr wrote: Probably the funniest of all pies is the kiwi pie. Of course, if Dan had been an adorable KDE hacker, it would have had to be a QT Pie. -- Chris Devers you may groan now ___ Boston-pm mailing list [EMAIL PROTECTED] http://mail.pm.org/mailman/listinfo/boston-pm
[Boston.pm] CGI::Carp and mod_perl
If I run this under mod_perl: #!/usr/bin/perl use CGI::Carp; die Death by chocolate\n; the Apache error log reads: [Tue Aug 3 17:54:00 2004] [error] [Tue Aug 3 17:54:00 2004] null: Death by chocolate If I run it without mod_perl, it prints out the name of my script instead of null. Why does it print null when run under mod_perl, and how can I fix this? ___ Boston-pm mailing list [EMAIL PROTECTED] http://mail.pm.org/mailman/listinfo/boston-pm