Uploading PDF

M

Mike

I'm writing a program that will let the user upload PDF files to the
server. I've done this with images before, and I figure that they're
both binary so I should be able to use the same script. But before I
do, I'm curious if I'm going to come across any problems with it.

$file = upload('file');

open INFILE, ">$base/$file";
binmode (INFILE);
while ($bytes = read($file,$data,16384)) {
$length_info += $bytes;
print INFILE $data;
}
close INFILE;

Should this work with any PDF file? I tested it with one file that I
had, but I'm not sure how PDFs actually work.

TIA,

Mike
 
D

David K. Wall

I'm writing a program that will let the user upload PDF files to the
server. I've done this with images before, and I figure that they're
both binary so I should be able to use the same script. But before I
do, I'm curious if I'm going to come across any problems with it.

$file = upload('file');

Are you not using strictures? You might regret that when your programs
start getting larger and more complex.

use strict;

will make you declare your variables and will also disable certain other
unsafe practices. 'use warnings;' is also highly recommended. Think of
them as safety equipment to catch mistakes. (they catch a lot of mine)
open INFILE, ">$base/$file";

You should always, yes *always*, check the return value from open():

open INFILE, ">$base/$file" or die "Cannot open $base/$file: $!";

(Hi Tad :)
binmode (INFILE);
while ($bytes = read($file,$data,16384)) {
$length_info += $bytes;
print INFILE $data;
}
close INFILE;

Should this work with any PDF file? I tested it with one file that I
had, but I'm not sure how PDFs actually work.

How PDF files work doesn't have anything to do with it. It's just binary
data that you're saving to a file.

Except for the $length_info line, the code looks as if it came almost
verbatim from the CGI.pm docs. Unless Lincoln Stein has really screwed
up I would expect it to work flawlessly (unless you have an error
somewhere else).
 
A

A. Sinan Unur

(e-mail address removed) (Mike) wrote:

You should always, yes *always*, check the return value from open():

open INFILE, ">$base/$file" or die "Cannot open $base/$file: $!";

While we are at it, why not use lexical file handles and the 3-argument
form of open:

use File::Spec;

open my $infile, '>', File::Spec->catfile($base, $file)
or die 'Cannot open ', File::Spec->catfile($base, $file), ": $!";

I typed the code above right into the newsreader so watch out for typos.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top