Re: CGI File upload, resulting files 0 bytes long
by Corion (Patriarch) on Mar 07, 2001 at 01:47 UTC
|
Two things - One, always use binmode() when opening files for reading or writing. This dosen't hurt under Unix, and it's certainly necessary under Windows.
The second, and even more important thing, use CGI; ! CGI.pm will make uploading files to your server as easy as :
# Copy a binary file to somewhere safe
open (OUTFILE,">>/usr/local/web/users/feedback");
binmode OUTFILE;
while ($bytesread=read($filename,$buffer,1024)) {
print OUTFILE $buffer;
}
also including an enforcement of a maximum upload size and many nifty other things. CGI.pm is your friend !
| [reply] [d/l] |
|
open (OUTFILE,">$file") or die "Couldn't open '$file': $!\n";
| [reply] [d/l] |
|
| [reply] |
|
Yes, I get the same "between the lines" impression, but that doesn't change the fact that such advice is just wrong.
Always use binmode() when dealing with binary data in files and never use bindmode() when dealing with only text data in files.
If you read a binary file without binmode() in DOS and expect to find the text bits in better shape, then you will be surprised when the binary data contains CTRL-Z and your program refuses to read past that, no matter how much text appears later in the file.
I find particularly unfortunate the "Should this happen to you" bit. "Well, I know it is possible that dealing with binary files w/o binmode() might cause problems but you should really wait to fix such problems until such time as you stumble upon them."
I'm disappointed at how frequently discussions of binmode() say stuff like "Use this if you aren't on Unix and...".
-
tye
(but my friends call me "Tye")
| [reply] |
|
|
Hi, thanks for your help.
I have just tried it that way and still isn't working.
I tried printing out $bytesread but it seems to have no value. I also checked if the filename that gets passed to my code is correct and that's the case. Am I missing something?
| [reply] |
Re: CGI File upload, resulting files 0 bytes long
by AgentM (Curate) on Mar 07, 2001 at 01:59 UTC
|
| [reply] |
Re: CGI File upload, resulting files 0 bytes long
by fpi (Monk) on Mar 07, 2001 at 14:44 UTC
|
1. Make sure you are sending the ENCTYPE as "multipart/form-data" in your form
2. Make sure you are using CGI.pm to receive it.
3. It's not unreasonable to check if it works differently with IE vs. Netscape, and what version you are using. The cgi.pm docs say that inconsistencies arise with different combinations of platform/webserver/browser.
I had the exact same problem with a MacOS9/WebSTAR system, so I looked at CGI.pm more closely. The docs say that the problem could be in the creation of the temp file, which CGI.pm creates with your upload, then transfers it to your specified destination. In my case, I was able to determine that a temp file was being created, but still only 0 bytes .But I was sure that the file was being uploaded by (transferred to) the webserver, just not saved at all. Some said the OS was unable to transfer large files, but my best guess had to do with the CGI glue from the webserver program to perl... | [reply] |
Re: CGI File upload, resulting files 0 bytes long
by wardk (Deacon) on Mar 07, 2001 at 02:29 UTC
|
the code looks alright. but you didn't include the
code that gave you the $data filehandle. be sure that
$data was retrieved something like so:
my $data = $query->param("uploadfileTagname");
a common gotcha is opening the uploaded filehandle for read, then
trying to write it back out. at least it "got" me....once :-)
| [reply] [d/l] |
|
I get my $data like this
my $data = $cgi -> param ( 'data' );
Give it to my subroutine like this
&receive_upload ( $data );
And receive the value in my subroutine like this
my $data = shift;
| [reply] [d/l] [select] |
|
from perlfaq5...
For passing filehandles to functions, the easiest way is to preface
them with a star, as in func(*STDIN). See the section on Passing
Filehandles in the perlfaq7 manpage for details.
from perlfaq7...
How can I pass/return a {Function, FileHandle, Array, Hash, Method,
Regexp}?
With the exception of regexps, you need to pass references to these
objects. See the section on Pass by Reference in the perlsub manpage
for this particular question, and the perlref manpage for information
on references.
| [reply] |
Re: CGI File upload, resulting files 0 bytes long
by markjugg (Curate) on Mar 07, 2001 at 07:47 UTC
|
I've got a guess here, but because it sounds like a wall I've run into before: This might not be a perl
problem at all. Did you set the encoding to be something like
"multipart/form-data" in your HTML <FORM> tag? There's
more documentation on this in the CGI.pm docs.
-mark | [reply] |
|
I'd put my money on this solution. If you didn't specify the correct mime-type when posting the file from the upload form, then there's nothing for the Perl script to accept.
| [reply] |
Re: CGI File upload, resulting files 0 bytes long
by Stamp_Guy (Monk) on Mar 07, 2001 at 05:21 UTC
|
Xitami? That could be your problem right there. I had a terrible with CGI on Xitami so I switched to Sambar. Try that out and see if it fixes your problem. You might also want to post the entire upload script so we can see exactly what's going on. I had this problem before, so if you need some further help on it, go ahead and email me here. | [reply] |