CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
File uploading

 



Anthony
Deleted

Sep 13, 2000, 12:53 PM

Post #1 of 9 (2202 views)
File uploading Can't Post

Crazy stuff, it should be working but the file created is zero bytes, as if $imgfile is empty, so there is no $data to print to the file.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


#!/usr/bin/perl

use CGI qw/:standard/;
#$CGI::POST_MAX=1024 * 50; # max 25K posts

if (!param()) {
print header();
print qq(
<form action="upload.cgi" method="post">
name:<input type="text" name="imgname"><br>
image:<input type="file" name="imgfile"><br>
<input type="submit">
</form>
);
}
else {
$imgfile = param('imgfile');
$imgname = param('imgname');

open (MYFILE, ">>/www/logos/$imgname") or die $!;
while ($bytes_read=read($imgfile,$buff,2096))
{
print MYFILE $buff;
}
close (MYFILE);

print "Content-type: text/html\n\n";
print "oh yeah";

}
</pre><HR></BLOCKQUOTE>


Anthony
Deleted

Sep 14, 2000, 5:23 AM

Post #2 of 9 (2202 views)
Re: File uploading [In reply to] Can't Post

The most important block here is the while block that pushes the reads the file and pushes the buffer into the filehandle, like so:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


while (my $bytesread = read($file, my $buffer, 1024)) {
$bytes = $bytesread;
$kb += ($bytes / 1000);
$donepage = "http://";
print OUTFILE $buffer;
}
</pre><HR></BLOCKQUOTE>

The rest is really a difference in the forms and shouldn't matter to the upload. So, I took your while block, used it, and the file is created empty (still that is).

I don't set the $donepage variable, but outside the block where the close() happens, I print Location as an arbitrary link (raging.com), which works just fine so I assume that the script is working. However, the file is created with zero bytes, unfortunately.

My assumptions:

1) A parameter I am not aware of in the input box could be missing because the browser doesn't seem to actually be sending the data.

2) Permissions on the directory are somehow not correct, though I see no error in the logs.

3) I am an idiot and there is something -really- simple and small I am missing.

My bet is 3. Smile

BTW, setting the Content-type header, then a Location header, shouldn't redirect, it just prints "Location:" to the page. Or are you doing this for a reason?


Anthony
Deleted

Sep 14, 2000, 5:57 AM

Post #3 of 9 (2202 views)
Re: File uploading [In reply to] Can't Post

Okay, so it seemed to be Assumption No. 1:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


#!/usr/bin/perl

use CGI qw/:all/;

$query = new CGI;

if (!param()) {
print header();
print $query->start_multipart_form('post','upload.cgi');
print $query->textfield('imgname','',20,50);
print "<br>";
print $query->filefield('imgfile','',20,80);
print "<br>";
print $query->submit();
print $query->endform();
}
else {
$imgname = $query->param('imgname');
$imgfile = $query->param('imgfile');

# Copy a binary file to somewhere safe
open (OUTFILE,">/www/logos/$imgname");
while ($bytesread=read($imgfile,$buffer,1024)) {
print OUTFILE $buffer;
}
close (OUTFILE);
print "Location: http://www.raging.com\n\n";
exit;
}

</pre><HR></BLOCKQUOTE>

Setting the encoding type to multipart (using the CGI interface to form building, which I plan to change) worked out just fine. Thanks for your help perlkid.

Oh, since the site helped me so much, I'll plug Perldoc.com (http://www.perldoc.com). All (or at least nearly all) the perl docs, including a load of module docs from CPAN. All HTMLized, easy to search, fast and readable. Kudos to Carlos Ramirez.

--
Anthony Bouvier
Vagabond Philosopher
--


perlkid
stranger

Sep 14, 2000, 10:11 AM

Post #4 of 9 (2202 views)
Re: File uploading [In reply to] Can't Post

 
Since You know your stuff,

I'll just give you the program I made. Compare and contrast.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


#!/usr/bin/perl
use CGI qw(:standard);
$q = new CGI;
$dir = $q->param('dir');
$basedir = "$dir";
$allowall = "no";
$type1 = ".gif";
$path = $q->param('FILE');
$name = $q->param('account');
$user = $q->param('name');
$filename = $q->param('filename');

use CGI;
$onnum = 1;

while ($onnum != 11) {
my $req = new CGI;
my $file = $req->param("FILE$onnum");
if ($file ne "") {
my $fileName = $file;
$fileName =~ s!^.*(\\|\/)!!;
$newmain = $fileName;
if ($allowall ne "yes") {
$exe = lc(substr($newmain,length($newmain) - 4,4));
if ($exe ne "$type")
{
$filenotgood = "yes";
$donepage = "http://";
}}

if ($filenotgood ne "yes") {
open (OUTFILE, ">$basedir/$fileName");
print "$basedir/$fileName<br>";
while (my $bytesread = read($file, my $buffer, 1024)) {
$bytes = $bytesread;
$kb += ($bytes / 1000);
$donepage = "http://";
print OUTFILE $buffer;
}
close (OUTFILE);
}
}
$onnum++;
}
print "Content-type: text/html\n";
print "Location:$donepage\n\n";
exit;
</pre><HR></BLOCKQUOTE>

One Tip,

I never got this working when I printed on the same cgi.

I had to print location in order to make a confirmation that the file was uploaded.

Try that first.

perlkid


perlkid
stranger

Sep 14, 2000, 12:10 PM

Post #5 of 9 (2202 views)
Re: File uploading [In reply to] Can't Post

 
Good For You Anthony,

perlkid


Anthony
Deleted

Sep 14, 2000, 3:40 PM

Post #6 of 9 (2202 views)
Re: File uploading [In reply to] Can't Post

And thanks for your help, perlkid.

Curious though, no example of the 'upload' input element I found on the web, mentioned encoding. I figure it may be peculiar to my server.

well, thanks again.


Merlin
journeyman

Sep 25, 2000, 5:07 PM

Post #7 of 9 (2202 views)
Re: File uploading [In reply to] Can't Post

Ok, guys, I need help here. I've been trying for days to get a script to upload jpg images via Perl for a client's catalog and I'm dying here... I'm not a pro (far from one!) and I only want this to work. I have little breakthrough here but it's not quite there. The code I use (modified a bit from your examples) :
#!/usr/bin/perl
use CGI qw/:standard/;
$objetcgi = new CGI;
$file = $objetcgi->param('image');
$imgname = "testimage.jpg";
open (MYFILE, ">$imgname") or die $!;
while (my $bytesread = read($file, my $buffer, 1024)) {
$bytes = $bytesread;
$kb += ($bytes / 1000);
print MYFILE $buffer;
}
close (MYFILE);
print "Content-type: text/html\n\n";
print "oh yeah";

I'm using cgi-bin since it seems to be the only place I can write to directly. I guess I'll rename the file to somewhere else via a script afterwards. For now, I got the script running and the testimage.jpg file is created but I get 0 bytes. Anything about why this happens?
Also, why can't I write to other dirs? Does this have to do with permissions? I tried dumping to a dir called temp but no go. The dir is on permission 755. I tried 777 also but I can't even list the dir then (don't know why). TIA!

Afterthought... :
Ok, could the 0 bytes written be a problem since I print after the dump? You said something about not being able to print with those type of scripts. Is this a bug or is there a way to do HTML print after the dump? I need to give a page that says that the upload is complete... Thanks!

[This message has been edited by Merlin (edited 09-25-2000).]


Anthony
Deleted

Sep 25, 2000, 10:06 PM

Post #8 of 9 (2202 views)
Re: File uploading [In reply to] Can't Post

When I was initially making this script, the file was being created with 0 bytes, just like your problem. Nothing in particular (upon cursory examination) is wrong with your syntax.

What you may want to do is set your form up like so (if you are not using the CGI module to build the HTML):

<form action="dir/to/yourscript.cgi" method="post" enctype="multipart/form-data">

Settingt the enctype is what solved my problem. Everything else you have seems fine.

And you should be able to print after the 'dump' -- above I set it to redirect to another site, but this isn't necessary. In my current script, redirection to a 'upload complete' page occurs. Setting the enctype should solve your problem.

Do you 'own' the directory 'temp'? Watch your permissions. If you set the directory (assuming you are owner) with execute permissions and do not later filter to make sure the file really IS an image, a malicious person (or even just a stupid one) could cause serious harm.

--
Anthony Bouvier
just another perl wanabe


[This message has been edited by Anthony (edited 09-26-2000).]


Merlin
journeyman

Sep 26, 2000, 3:22 PM

Post #9 of 9 (2202 views)
Re: File uploading [In reply to] Can't Post

I should have specified this firsthand... The form was already set to multipart so I guess it's back to the drawing board... Any other suggestions? I did have another more complicated script I used from a guy called Simon Tneoh and it did write the file to cgi-bin so it is feasable. I had too much trouble integrating his script into mine so I gave up. Your little script is far more easy to manage and use. Thanks and let me know if you have something else about this. I'll look into it from my side.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives