Home > Archive > PERL CGI Beginners > August 2005 > upload question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Cristi Ocolisan 2005-08-25, 3:55 am |
| Hi all,
First, because I'm new to this list, I want to salute you all.
Second, please help me understand where my error is .
Does anybody know why this code does NOT copy the file into the specified
directory?
After the script is executed, my file is listed into the directory, but
empty.
Strange?
my $upload_dir = "../../witp/sigle_firme";
my $max_size = 1024_000;
my $file_name;
if ( length($sigla) > 0 ) {
( $file_name = $sigla ) =~ s/.*\\//;
my $upload_path = "$upload_dir/$file_name";
open (OUT, ">$upload_path") or die "Error opening $upload_path: $!";
binmode OUT;
my $buffer = '';
my $size = 0;
while (my $chars_read = read($sigla, $buffer, 4096)) {
print OUT $buffer;
$size += $chars_read;
if ( $size > $max_size ) {
last;
}
}
close OUT;
if ( -z $upload_path or $size > $max_size ) {
unlink $upload_path;
}
}
It is strange because I wrote this code earlier and it was working
perfectly.
Please make me understand why it does not want to work anymore :-).
Thanks.
CO
| |
|
| --- Cristi Ocolisan <cristi@wiq.ro> wrote:
> Does anybody know why this code does NOT copy the file into the
> specified directory?
>
> After the script is executed, my file is listed into the directory,
> but empty.
>
> Strange?
You indicate that this script used to work. That suggests to me that
someone was doing maitenance that broke it. Find out who and why :)
That being said, the problem appears to be in your while loop:
> if ( length($sigla) > 0 ) {
> ( $file_name = $sigla ) =~ s/.*\\//;
<snip>
> while (my $chars_read = read($sigla, $buffer, 4096)) {
> print OUT $buffer;
> $size += $chars_read;
> if ( $size > $max_size ) {
> last;
> }
> }
The first argument to read() should be a filehandle, not a filename.
From "perldoc -f read":
read FILEHANDLE,SCALAR,LENGTH,OFFSET
read FILEHANDLE,SCALAR,LENGTH
Attempts to read LENGTH characters of data into variable SCALAR
from the specified FILEHANDLE.
Cheers,
Ovid
--
If this message is a response to a question on a mailing list, please send
follow up questions to the list.
Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/
|
|
|
|
|