in reply to Re: Re: Re: Upload Question
in thread Upload Question

Here is where I am at now:

#!/usr/bin/perl -w use strict; use CGI; use Fcntl; my $formdata = CGI->new; my $file = $formdata->param('file'); my $filename = $formdata->param('filename'); #user supplied fi +le name my $fh = $formdata->upload('file'); my $upload_dir = "/var/www/html/eface/client_pics/"; my $buffer = ""; print $formdata->header(); print STDERR "$file\n"; print STDERR "$filename\n"; if (defined $fh){ print STDERR "\$fh is defined\n"; } # Open the file to be wriiten into the upload directory sysopen OUTPUT, $upload_dir . $filename, O_CREAT | O_EXCL; # Write file to upload directory while (read ($fh, $buffer, 16384)){ print OUTPUT $buffer; } close OUTPUT;

This code still gives me the file named $filename in my upload directory $upload_dir but it still contains 0K byte size.

Now when I run in my code:

while (<$fh>){ print; }

it does print out data to the browser so I know that the temporary file is being found. I can't quite solve this. Any Ideas?

Thanks Again for everyones help.

Joe

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Upload Question
by antirice (Priest) on Nov 04, 2003 at 02:58 UTC

    I don't know why you're using sysopen for this, but ok. It's possible since you're using O_EXCL, the sysopen is failing. Straight from perldoc perlopentut:

    O_EXCL - Fail if the file already exists

    So if the file's already there. Sysopen will fail. Another problem is the fact that sysopen is opening the file for READING ONLY. If you change it to O_CREAT | O_EXCL | O_WRONLY, the file will be open for writing. Finally, you always want to know if the open failed. This can be done quite simply by adding or die $!; to the end. So in summation, you actually want the following line for your sysopen:

    sysopen(OUTPUT, $upload_dir . $filename, O_CREAT | O_EXCL | O_WRONLY) +or die $!;

    Now please note that if someone tries to upload this file again, the upload will fail. Since the die doesn't present any error message to the end user himself, you may instead want to use -e whick will tell you whether or not the file or directory already exists. You can read more about opening files at perldoc perlopentut as well as file detection switches at perldoc -f -X.

    Hope this helps.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      Thanks sooooo much for the help and direction. I knew I had a write problem but I guess I didn't understand the documentation in the Camel book. The perlopentut cleared it up for me.

      Just one more clarification, are you saying that it is better to use open instead of sysopen? I had chosen the latter because I thought that open required that the file exist. I now now better.

      Thanks again

      Joe

        From perldoc perlopentut

        If you want the convenience of the shell, then Perl's open is definitely the way to go. On the other hand, if you want finer precision than C's simplistic fopen(3S) provides, then you should look to Perl's sysopen, which is a direct hook into the open(2) system call. That does mean it's a bit more involved, but that's the price of precision.

        In this case, I just don't see the justification for using sysopen instead of regular open. It doesn't make much of a difference either way. When I review someone else's code and see where they use sysopen, alarms usually go off in my head and I immediately attempt to determine why they'd use it. But still, it doesn't matter either way.

        Hope this helps.

        antirice    
        The first rule of Perl club is - use Perl
        The
        ith rule of Perl club is - follow rule i - 1 for i > 1