Trouble with a file upload script

S

Suk

Hi

I am experimenting with a very simple file upload script.

I cannot understand why the script below doesnt work:
My web server log shows:

Modification of a read-only value attempted at /var/apache/cgi-bin/
uploader.cgi line 94.

Line 94 is: while ( <$upload_filehandle> )
Here is the script.....

#/usr/local/bin/perl
#
use strict;
use CGI qw:)standard);

our $upload_dir = "/tmp";
our ($upload_file,$success,$successfully_uploaded,$count);

foreach ("new","outstanding","weekly","year") {

if (param("$_") ne "") {

&upload_and_install("$_");
}
}

print header();
print <<END_HTML;

<HTML>
<HEAD>
<TITLE>Thanks!</TITLE>
</HEAD>

<BODY>

<P>Thanks for uploading your file</P>

</BODY>
</HTML>

END_HTML

sub upload_and_install {

my ($filename,$upload_filehandle);

$filename = param($_[0]);
$filename =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle = upload($_[0]);

open UPLOADFILE, ">$upload_dir/$filename";

binmode UPLOADFILE;

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

close UPLOADFILE;
}


However I try the same script but test each parameter invidually
instead of using the foreach loop, and the script does work: So
replacing the foreach loop with this:

if (param("new") ne "" ) {
&upload_and_install("new");
}

if (param("outstanding") ne "" ) {
&upload_and_install("outstanding");
}

if (param("weekly") ne "" ) {
&upload_and_install("weekly");
}

if (param("year") ne "" ) {
&upload_and_install("year");
}

And then I get no errors... Any ideas anyone?
 
M

Mumia W.

Hi

I am experimenting with a very simple file upload script.

I cannot understand why the script below doesnt work:
My web server log shows:

Modification of a read-only value attempted at /var/apache/cgi-bin/
uploader.cgi line 94.

Line 94 is: while ( <$upload_filehandle> )
Here is the script.....

#/usr/local/bin/perl
#
use strict;
use CGI qw:)standard);

our $upload_dir = "/tmp";
our ($upload_file,$success,$successfully_uploaded,$count);

foreach ("new","outstanding","weekly","year") {

if (param("$_") ne "") {

At this point, $_ is an alias for "new" which is a constant (read-only)
string.
&upload_and_install("$_");
}
}

print header();
print <<END_HTML;

<HTML>
<HEAD>
<TITLE>Thanks!</TITLE>
</HEAD>

<BODY>

<P>Thanks for uploading your file</P>

</BODY>
</HTML>

END_HTML

sub upload_and_install {

my ($filename,$upload_filehandle);

$filename = param($_[0]);
$filename =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle = upload($_[0]);

open UPLOADFILE, ">$upload_dir/$filename";

binmode UPLOADFILE;

while ( <$upload_filehandle> )

This attepts to put the data read from the file-handle into $_ which is
aliased to the "new" string above, and since that string is read-only,
the operation fails.

To solve this problem, at the top of upload_and_install, put this line:

local $_;

That creates a block-local $_ for you to play with without interfering
with the for loop's $_.

Do this to get more information about "local":
Start->Run->"perldoc -f local"

This is also an opportunity for you to decide to use named variables
rather than the default $_, e.g:

while (my $part = <$upload_filehandle>) {
...
}

Note, I haven't run your program. My assumption of the error comes from
a quick perusal of the program.

{
print UPLOADFILE;
}
[...]
 
J

john.swilting

Mumia said:
Hi

I am experimenting with a very simple file upload script.

I cannot understand why the script below doesnt work:
My web server log shows:

Modification of a read-only value attempted at /var/apache/cgi-bin/
uploader.cgi line 94.

Line 94 is: while ( <$upload_filehandle> )
Here is the script.....

#/usr/local/bin/perl
#
use strict;
use CGI qw:)standard);

our $upload_dir = "/tmp";
our ($upload_file,$success,$successfully_uploaded,$count);

foreach ("new","outstanding","weekly","year") {

if (param("$_") ne "") {

At this point, $_ is an alias for "new" which is a constant (read-only)
string.
&upload_and_install("$_");
}
}

print header();
print <<END_HTML;

<HTML>
<HEAD>
<TITLE>Thanks!</TITLE>
</HEAD>

<BODY>

<P>Thanks for uploading your file</P>

</BODY>
</HTML>

END_HTML

sub upload_and_install {

my ($filename,$upload_filehandle);

$filename = param($_[0]);
$filename =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle = upload($_[0]);

open UPLOADFILE, ">$upload_dir/$filename";

binmode UPLOADFILE;

while ( <$upload_filehandle> )

This attepts to put the data read from the file-handle into $_ which is
aliased to the "new" string above, and since that string is read-only,
the operation fails.

To solve this problem, at the top of upload_and_install, put this line:

local $_;

That creates a block-local $_ for you to play with without interfering
with the for loop's $_.

Do this to get more information about "local":
Start->Run->"perldoc -f local"

This is also an opportunity for you to decide to use named variables
rather than the default $_, e.g:

while (my $part = <$upload_filehandle>) {
...
}

Note, I haven't run your program. My assumption of the error comes from
a quick perusal of the program.

{
print UPLOADFILE;
}
[...]
use CGI::UploadEasy its very nice and simple
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top