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: Beginner:
Upload

 



Pro_4
User

May 10, 2001, 1:51 PM

Post #1 of 7 (1256 views)
Upload Can't Post

Well i thought i had gotten rid of all my problems with uploading but it turns out that i all i was doing was creating a file with the filename writen in it. So my question is how do i extract the data from the file the user is trying to upload and put it into the file i choose. I attached the uploader i am using.

People Engrossed by a Reliable Language


rjoseph
Novice

May 11, 2001, 4:04 PM

Post #2 of 7 (1236 views)
Re: Upload [In reply to] Can't Post

Ok, there is a couple of things wrong here - CGI uploading is different than just writing a regular file. Here is a snippet from your code:

Code
if (( $file_ext eq 'scn' ) || ( $file_ext eq 'scx' )) { 
open (DIR, "+>> scenarios/$file ");
print DIR $file;

Here is how it needs to be done to work:

Code
if (( $file_ext eq 'scn' ) || ( $file_ext eq 'scx' )) { 
my $buffer;
open (DIR, "+>> scenarios/$file ");
binmode(DIR);
while( read(DIR, $buffer, 1024) ) { print DIR $buffer; }
close(DIR);

The $file varible actually just holds the name of the file on the users computer, not the actual file contents. Therefore, you need to 'read' the file from the users computer (which is actually a whole mess of things that happens with the browser and what not, but that isn't important) and then write it to your server/hard drive/whatever. However, because you don't know what format the file is going to be in, you need to open it and then use binmode(), and then use data in chunks with the read() function.

One more little tip - I wouldn't suggest using DIR as a filehandle - it confuses people (including me) because they (and I) think that you are reading from a directory. Just a thought.

Hope this helps!

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


Pro_4
User

May 11, 2001, 4:34 PM

Post #3 of 7 (1232 views)
Re: Upload [In reply to] Can't Post

Hmm i tried that and all it did was the same as before except that nothing at all was in the file. I changed the filename from DIR to FILE so i wont confuse people ( when i was learning uploading, DIR was in the example so i just kinda stuck with that ). Here is the code for those who are to lazy to download the file.

Code
#!/usr/bin/perl -w 
use CGI qw(:all);
print header;
$file = param('file'); chomp $file;
$file =~ s!^.*(\\|/)!!;
$file=~ tr/ /_/;
($file_ext = $file) =~ s/.*?\.(\w*)/$1/ig;
if (( $file_ext eq 'scn' ) || ( $file_ext eq 'scx' )) {
my $buffer;
open (FILE, "> scenarios/$file ");
binmode(FILE);
while( read(FILE, $buffer, 1024) ) { print FILE $buffer; }
close(FILE);
open (DATA, '+>> scenarios/scenarios.txt');
my $desc= "N/A";
my $path="Http://etow.virtualave.net/cgi-bin/scenarios/$file";
my $info="$file|$desc|$path\n";
print DATA $info;
close(DATA);
print "<body background=\'BackLwood.jpg'>";
print "<p>Done Uploading Your File:</p><p>$file</p><p>Thank You :)</p>";
} else {
print "Sorry we only except file extensions with .scn
or .scx if you zipped your file please unzip it."
}

Thanks in advance :) Also could someone please explain the part in red to me. Especially the 1024 what does that mean?

People Engrossed by a Reliable Language


freddo
User

May 12, 2001, 7:05 AM

Post #4 of 7 (1224 views)
Re: Upload [In reply to] Can't Post

Hi Pro4,

Here you will find the doc for read, what it just do is read the an arbitrary length of bytes (1024 in the previous example, 2^10) and put it into $buffer. To understand better i written you this little sample:

Code
#/usr/bin/perl 

# Let's populate a file
$content = "The Gathering of Kings " x 5;
print "The file size will be ", length($content), "bytes\n\n";

open FILE, "> sample.txt";
print FILE $content;
close FILE;

# Let's see what read does
open FILE, "< sample.txt";
while ( $howmuch = read(FILE, $buffer, 16)) {
print "Read $howmuch byte(s). Buffer is [$buffer]\n"
}
close FILE;

What you do with the line while( read(FILE, $buffer, 1024) ) { print FILE $buffer; } should now seems pretty funny, you open a file for writting, expect to find something in it.... and then try to print it back Tongue

You might want to check the CGI.pm documentation about CREATING A FILE UPLOAD FIELD. In few words, it is resumed like this (but dont take my word on it, i've never used cgi.pm):

When the form is processed, you can retrieve the entered filename by calling param():
$filename = $query->param('uploaded_file');

The filename returned is also a file handle. You can read the contents of the file using standard Perl file reading calls:
# Read a text file and print it out
while (<$filename>) {
print;
}


or

# Copy a binary file to somewhere safe
open (OUTFILE,">>/usr/local/web/users/feedback");
while ($bytesread=read($filename,$buffer,1024)) {
print OUTFILE $buffer;
}


So with that in mind your script should become quite more easier Smile

i hope this helps
freddo

;---
Real programmers don't eat cache.


rjoseph
Novice

May 12, 2001, 11:49 AM

Post #5 of 7 (1213 views)
Re: Upload [In reply to] Can't Post


In Reply To
What you do with the line while( read(FILE, $buffer, 1024) ) { print FILE $buffer; } should now seems pretty funny, you open a file for writting, expect to find something in it.... and then try to print it back

Kudos freddo - It was late and I didn't even catch that one!

Pro_4, he explained it really well, so I hope that now you can get your script working!! Have fun, and thanks freddo for catching my mistake!

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


freddo
User

May 12, 2001, 12:55 PM

Post #6 of 7 (1209 views)
Re: Upload [In reply to] Can't Post

Np -- Joseph,

Your answer was good anyway, except for that little mistake, and those little points i wanted you to notice in your strange explaination:

In Reply To
.... The $file varible actually just holds the name of the file on the users computer (1), not the actual file contents. Therefore, you need to 'read' the file from the users computer (which is actually a whole mess of things that happens with the browser and what not, but that isn't important) and then write it to your server/hard drive/whatever (2). ...

the fact (taken from the CGI.pm Documentation) is that:

The filename returned is also a file handle. (1). You can read the contents of the file using standard Perl file reading calls... and: CGI.pm can process uploaded file. Ordinarily it spools the uploaded file to a temporary directory (2), then deletes the file when done. However, this opens the risk of eavesdropping as described in the file upload section... (read more) -- and you can ask me about race conditions if you wish... Wink

i mean no offense, it is just because iwas reading cgi.pm while replying to Pro4, in three days i'll probably have forgotten the whole thing... Smile

btw, what the hell is "Body-Boarding"?

later
freddo

;---
Real programmers don't eat cache.


rjoseph
Novice

May 12, 2001, 3:49 PM

Post #7 of 7 (1206 views)
Re: Upload [In reply to] Can't Post

No offense taken, I am glad that someone corrected me - I try to be infalible, but it is hard being a God sometimes Smile.

Thanks also for posting the actual CGI.pm explanations - I guess what I wrote sounded much more organized in my head than it did when I wrote it out! Also, I had meant to go and actually read the docs, but I never got around to it. Again, no offense taken at all, in fact, I appreciate it!

And as a side-note, body-boarding is probably the best water sport (actually, the best sport period) ever invented, IMHO Smile. It is somewhat like surfing, but you lay down on a shorter and wider board, which opens up the opprotunity for many more interesting trick. You might also have heard it called "boogie-boarding" because a company named Morey created one of the first boards for the sport and called the board the "Morey Boogie" - hence the common name of "boogie boarding" (kinda like in-line skating is called "rollerblading" thanks to Rollerblade Inc.) So there you go - body boarding :).

Again, thanks a ton for you help!

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation

 
 


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

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