Hi Mike.

Mike Garner wrote:
> Hello all-
> I'm trying to write a script that will upload a file from a web page
> and store that file on the web server. I've snipped out the piece of
> code below that's supposed to do this for me.  This is running under
> use CGI ':standard'; use strict; and use warnings without any errors.
>
> # $file is the full path of the file and file name submitted from the
> form. # $upload_path looks like d:/upload_files/
>
> I believe the problem is in the while loop.  The conditional if
> returns true and the file gets created  with the correct name in the
> correct path (so I know the permissions are correct on the server)
> but I never see the "Hello' prints in the browser and the file never
> grows past 0kb.  For some reason I guess it isn't reading in the file
> from the client browser.  What have I missed? I checked a couple
> locations on CPAN for upload scripts and they perform the same basic
> operation. I even snipped parts of those scripts and used them in
> place of my upload process but I've arrived at the same result. I'd
> appreciate any direction you may have.
>
> ###---Here are the relevant secions of code
>
> print "Content-type: text/html\n\n";
> print "<html>\n";
> print "<head>\n";
> print "<body>\n";
> if ($file) {
>   my @fileparts=split(/\\/,$file);
>   my $file_name=pop(@fileparts);

    my $file_name = (split '\', $file)[-1];

>   open (UPLOAD, ">$upload_path/$file_name") || die "Can't open
file\n";

You need to be careful with your path separators here. You've
used both slash and backslash in the open and the split. Which
should it be?

Also, if you

    die "Can't open file: $!";

you'll get the file system error message in the log, as well
as the source file and line number (which aren't output
if you put a \n at the end of your text).

>   my ($data,$length,$chunk);
>   while ($chunk = read ($file,$data,1024)) {

OK, now you're trying to read from $file which has to be a
filehandle. Since you say the preceding code works you must
have a filename in $file, so you need to do something like

    open my $fh, $file, "<", $file or die $!;

to get a useable filehandle.

>     print UPLOAD $data;

Check whether you need 'binmode' on each handle.

>     print "Hello!<br>\n";
>   }

It would be nice to write

    die "Read error" unless defined $chunk;

so that you know if your read has failed. $chunk will
be zero, and therefore false but defined, if the read
terminates normally at eof.

>   close UPLOAD;
> }

Cheers Mike,

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to