Gossamer Forum
Home : General : Perl Programming :

CGI.PM UPLOAD PROBLEM

Quote Reply
CGI.PM UPLOAD PROBLEM
Hello... I'm having a problem with CGI.PM upload function:

--------
$image = $q->param('image');
open(IMGFL,">$imageloc");
while ($bytesread=read($image,$buffer,1024)){
print IMGFL $buffer;
}
close(IMGFL);
--------

Binary files are broken ... it looks like it is uploading in text mode (i noticed this with images)

ideas?
Quote Reply
Re: [robyone] CGI.PM UPLOAD PROBLEM In reply to
You aren't using CGI.pm to upload :)

Use:

Code:
my $image = $q->param('image');

open(IMGFL,">$imageloc") or die $!;
binmode (IMGFL); # You need this for windows
while (<$image>) { # Much nicer :)
print IMGFL;
}
close IMGFL;

Last edited by:

Paul: Apr 12, 2002, 4:55 AM
Quote Reply
Re: [robyone] CGI.PM UPLOAD PROBLEM In reply to
http://stein.cshl.org/.../CGI/#upload_caveats

- wil
Quote Reply
Re: [Paul] CGI.PM UPLOAD PROBLEM In reply to
Hi Paul!

Your code works great on windows ... THANKS!!!!
but I'm having the same problem on unix.
I tried to remove the binmode ...no results :(

Ideas? How can I make it work on a nix o.s.?
Quote Reply
Re: [robyone] CGI.PM UPLOAD PROBLEM In reply to
What's the error message? It should work on Unix. Maybe you should check file permissions, or even better CHMOD the file before printing to it.

This is the 'correct' CGI.pm way of doing things.

Code:
$filename = $query->param("filename");
$filename =~ s/.*[\/\\](.*)/$1/;

chmod 0777, "$filename";

$upload_filehandle = $query->upload("filename");

open UPLOADFILE,">$filename";

while ( <$upload_filehandle> ) {
print UPLOADFILE;
}

close UPLOADFILE;

- wil

Last edited by:

Wil: Apr 12, 2002, 6:03 AM
Quote Reply
Re: [Wil] CGI.PM UPLOAD PROBLEM In reply to
wil,

the problem is that it is uploading in text mode ... resulting that images are broken.

i don't get any server error. the file goes up to the server and the file-size is correct .
Quote Reply
Re: [robyone] CGI.PM UPLOAD PROBLEM In reply to
I use that code I gave you and it works fine on linux and windows.
Quote Reply
Re: [robyone] CGI.PM UPLOAD PROBLEM In reply to
But the method Paul shows you doesn't upload anything in ASCII or BINARY mode, that all is does is write the data to a file. It's up to you what extension you put on the file after, maybe that's the problem?

- wil
Quote Reply
Re: [Wil] CGI.PM UPLOAD PROBLEM In reply to
>>But the method Paul shows you doesn't upload anything in ASCII or BINARY mode<<

Yes it does, otherwise I would have wasted my time adding in binmode() Cool

http://www.perldoc.com/...od/func/binmode.html

Last edited by:

Paul: Apr 12, 2002, 6:26 AM
Quote Reply
Re: [Paul] CGI.PM UPLOAD PROBLEM In reply to
Strange, because the solution I provided in this post works for me without any mention of binmode.

- wil
Quote Reply
Re: [Wil] CGI.PM UPLOAD PROBLEM In reply to
Take a look at the binmode link above...it explains when it is needed and when it isn't.

It is generally needed for compatibility......anyway, that wasn't the comment you made...you said the code I provided didn't affect the mode that the file was uploaded in, not whether it worked or not.
Quote Reply
Re: [Paul] CGI.PM UPLOAD PROBLEM In reply to
OK. thank you for your help ...

Code from Paul is great on windows but wil's code works for linux.

I guess we solved the problem. Thank you!!

Last edited by:

robyone: Apr 12, 2002, 7:35 AM
Quote Reply
Re: [robyone] CGI.PM UPLOAD PROBLEM In reply to
Wils code is practically the same as mine...the only difference is the binmode line. If you think that is causing you problems try chaging it to:

binmode(IMGFL) if $^O eq 'MSWin32';
Quote Reply
Re: [Paul] CGI.PM UPLOAD PROBLEM In reply to
And I'm changing permissions before writing.

- wil
Quote Reply
Re: [Wil] CGI.PM UPLOAD PROBLEM In reply to
Yes I was looking at that before...the code doesn't make sense...you are chmodding something non existant?

If you change your code to:

chmod (0777, $filename) or die;

....you'll probably find it dies Cool
Quote Reply
Re: [Paul] CGI.PM UPLOAD PROBLEM In reply to
ok works ... just tired :(((
thanks. but what about 'MSWin32'?
will it work fine always?

Last edited by:

robyone: Apr 12, 2002, 8:26 AM
Quote Reply
Re: [robyone] CGI.PM UPLOAD PROBLEM In reply to
The while loop works fine on linux, trust me :)

Do you want me to prove it?

Last edited by:

Paul: Apr 12, 2002, 8:23 AM
Quote Reply
Re: [robyone] CGI.PM UPLOAD PROBLEM In reply to
Yeah it will work.

Here's my little demo:

http://www.wiredon.net/cgi-bin/proof/index.cgi

http://www.wiredon.net/...f/index.cgi?source=1
Quote Reply
Re: [Paul] CGI.PM UPLOAD PROBLEM In reply to
Doesn't work for me - I upload and then get a 404 error while trying to view the file I uploaded :-)

- wil
Quote Reply
Re: [Wil] CGI.PM UPLOAD PROBLEM In reply to
Works fine for me...I tested about 15 times ;)

What filename did you enter?
Quote Reply
Re: [Paul] CGI.PM UPLOAD PROBLEM In reply to
This is working on win, unix, linux ...


open(IMGFL,">$imageloc");
binmode(IMGFL) if $^O eq 'MSWin32';
while (<$image>){print IMGFL;}
close(IMGFL);

Wink

RobyOne