I have problems with download scripts, it's trying to print instead of saving the file

Discussion in 'Perl Misc' started by E Arredondo, Sep 30, 2005.

  1. E Arredondo

    E Arredondo Guest

    Here's my cgi script :

    ---------------------------------------
    #!/usr/bin/perl -w

    use CGI;

    $upload_dir = "/upload";
    $query = new CGI;
    $filename = $query->param("photo");
    $claim = $query->param("claim");
    $filename =~ s/.*[\/\\](.*)/$1/;
    $upload_filehandle = $query->upload("photo");
    open UPLOADFILE, ">$upload_dir/$filename";
    binmode UPLOADFILE;
    while ( <$upload_filehandle> )
    {
    print UPLOADFILE;
    }
    close UPLOADFILE;
    print $query->header ( );
    print <<END_HTML;

    <HTML>
    <HEAD>
    <TITLE>Thanks!</TITLE>
    <HEAD>
    <TITLE>Thanks!</TITLE>
    </HEAD>
    <BODY>
    <P>Thanks for uploading your photo!</P>
    <P>Your Claim: $claim</P>
    </BODY>
    </HTML>

    END_HTML
    --------- cut here --------------------------------

    And here's the log file :
    binmode() on closed filehandle UPLOADFILE at
    /usr/lib/apache/cgi-bin/upload.cgi
    line 13.
    print() on closed filehandle UPLOADFILE at
    /usr/lib/apache/cgi-bin/upload.cgi li
    ne 16, <fh00001C%3A\Graphics\crank.JPG> line 1.
    print() on closed filehandle UPLOADFILE at
    /usr/lib/apache/cgi-bin/upload.cgi li
    ne 16, <fh00001C%3A\Graphics\crank.JPG> line 2

    keeps going........all the way until

    print() on closed filehandle UPLOADFILE at
    /usr/lib/apache/cgi-bin/upload.cgi li
    ne 16, <fh00001C%3A\Graphics\crank.JPG> line 433.
    print() on closed filehandle UPLOADFILE at
    /usr/lib/apache/cgi-bin/upload.cgi li
    ne 16, <fh00001C%3A\Graphics\crank.JPG> line 434.
    print() on closed filehandle UPLOADFILE at
    /usr/lib/apache/cgi-bin/upload.cgi li
    ne 16, <fh00001C%3A\Graphics\crank.JPG> line 435.
    UX:lp: ERROR: No default destination.
    TO FIX: You must identify which printer should
    handle your request by naming it or a
    class of printers (-d name) or by naming
    a type of printer (-T type).
    ----------------------- cut here ------------------------------------

    I can't find the file, so I'm assuming that it tried it to print , Am I
    right ?

    I'm running SCO OSR 507 (please don't flame me), and perl version 5.8.6EB

    Thanks
    E Arredondo, Sep 30, 2005
    #1
    1. Advertising

  2. E Arredondo

    Guest

    You should always check the return value of open(). Try:

    open(UPLOADFILE, ">$upload_dir/$filename")
    or die "Can't write to '$upload_dir/$filename': $!\n";

    I bet the webserver is running as a user that doesn't have permission
    to write to the specified directory.
    , Sep 30, 2005
    #2
    1. Advertising

  3. E Arredondo <> wrote:

    > #!/usr/bin/perl -w
    >
    > use CGI;



    You are missing a line that should be included in all of your Perl programs:

    use strict;


    > $filename = $query->param("photo");
    > $filename =~ s/.*[\/\\](.*)/$1/;



    perldoc File::Basename


    > $upload_filehandle = $query->upload("photo");



    That variable contains a *string*, not a filehandle...


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



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

    open UPLOADFILE, ">$upload_dir/$filename" or
    die "could not open '$upload_dir/$filename' $!";


    > while ( <$upload_filehandle> )



    You need to put an _actual_ filehandle inside the input operator.


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



    That is a rather bizarre document structure...


    > And here's the log file :
    > binmode() on closed filehandle UPLOADFILE at



    Your open() failed, but you didn't know it because you did
    not check its return value.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 30, 2005
    #3
  4. E Arredondo

    Paul Lalli Guest

    Tad McClellan wrote:
    > > $upload_filehandle = $query->upload("photo");

    >
    > That variable contains a *string*, not a filehandle...


    >From perldoc CGI:

    ==================
    To be safe, use the upload() function (new in version 2.47). When
    called with the name of an upload field, upload() returns a filehandle,
    or undef if the parameter is not a valid filehandle.

    $fh = upload('uploaded_file');
    while (<$fh>) {
    print;
    }
    ===================

    Paul Lalli
    Paul Lalli, Sep 30, 2005
    #4
  5. Re: I have problems with download scripts, it's trying to print insteadof saving the file

    E Arredondo wrote:
    >
    > use CGI;
    >
    > $upload_dir = "/upload";
    > $query = new CGI;
    > $filename = $query->param("photo");
    > $claim = $query->param("claim");
    > $filename =~ s/.*[\/\\](.*)/$1/;
    > $upload_filehandle = $query->upload("photo");
    > open UPLOADFILE, ">$upload_dir/$filename";
    > binmode UPLOADFILE;
    > while ( <$upload_filehandle> )
    > {
    > print UPLOADFILE;
    > }
    > close UPLOADFILE;
    > print $query->header ( );


    You may want to use the CPAN module CGI::UploadEasy. The above part of
    your script can be replaced with:

    use CGI::UploadEasy;
    my $ue = CGI::UploadEasy->new(-uploaddir => "/upload");
    my $cgi = $ue->cgiobject;
    my $claim = $cgi->param('claim');
    print $cgi->header;

    Now, as others have said, I doubt that you have a directory "/upload"
    under the server root. Maybe you mean "$ENV{DOCUMENT_ROOT}/upload".
    CGI::UploadEasy includes the necessary checks to find out.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 30, 2005
    #5
  6. Re: I have problems with download scripts, it's trying to print insteadof saving the file

    Tad McClellan wrote:
    > E Arredondo wrote:
    >>
    >> $filename = $query->param("photo");
    >> $filename =~ s/.*[\/\\](.*)/$1/;

    >
    > perldoc File::Basename


    Not for parsing the path that comes with a file upload request.
    File::Basename doesn't have a clue about the platform the file was
    uploaded from.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 30, 2005
    #6
  7. Re: I have problems with download scripts, it's trying to print insteadof saving the file

    Gunnar Hjalmarsson wrote:
    > Tad McClellan wrote:
    >> E Arredondo wrote:
    >>>
    >>> $filename = $query->param("photo");
    >>> $filename =~ s/.*[\/\\](.*)/$1/;

    >>
    >> perldoc File::Basename

    >
    > Not for parsing the path that comes with a file upload request.
    > File::Basename doesn't have a clue about the platform the file was
    > uploaded from.


    CGI::UploadEasy uses this expression to remove the non-filename part of
    the path:

    s#.*[\]:\\/]##

    Hopefully it's platform independent...

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 30, 2005
    #7
  8. E Arredondo

    E Arredondo Guest

    <> wrote in message
    news:...
    > You should always check the return value of open(). Try:
    >
    > open(UPLOADFILE, ">$upload_dir/$filename")
    > or die "Can't write to '$upload_dir/$filename': $!\n";
    >
    > I bet the webserver is running as a user that doesn't have permission
    > to write to the specified directory.
    >


    WOW, it's working now! I had the wrong path to the upload!! now I added
    /www/docs/upload.


    Thanks so much
    E Arredondo, Sep 30, 2005
    #8
  9. E Arredondo

    E Arredondo Guest

    "Tad McClellan" <> wrote in message
    news:...
    >E Arredondo <> wrote:
    >
    >> #!/usr/bin/perl -w
    >>
    >> use CGI;

    >
    >
    > You are missing a line that should be included in all of your Perl
    > programs:
    >
    > use strict;
    >


    I'm getting this on the log file after adding the strict command:

    Global symbol "$query" requires explicit package name at
    /usr/lib/apache/cgi-bin/upload.cgi line 11.
    Global symbol "$upload_dir" requires explicit package name at
    /usr/lib/apache/cgi-bin/upload.cgi line 13.
    Global symbol "$filename" requires explicit package name at
    /usr/lib/apache/cgi-bin/upload.cgi line 13.
    Global symbol "$upload_dir" requires explicit package name at
    /usr/lib/apache/cgi-bin/upload.cgi line 14.
    Global symbol "$filename" requires explicit package name at
    /usr/lib/apache/cgi-bin/upload.cgi line 14.
    Global symbol "$upload_filehandle" requires explicit package name at
    /usr/lib/apache/cgi-bin/upload.cgi line 16.
    Global symbol "$query" requires explicit package name at
    /usr/lib/apache/cgi-bin/upload.cgi line 22.
    Global symbol "$claim" requires explicit package name at
    /usr/lib/apache/cgi-bin/upload.cgi line 23.
    Execution of /usr/lib/apache/cgi-bin/upload.cgi aborted due to compilation
    errors.
    [Fri Sep 30 11:57:15 2005] [error] [client 69.239.42.161] Premature end of
    script headers: /usr/lib/apache/cgi-bin/upload.cgi


    >
    >> $filename = $query->param("photo");
    >> $filename =~ s/.*[\/\\](.*)/$1/;

    >
    >
    > perldoc File::Basename
    >
    >
    >> $upload_filehandle = $query->upload("photo");

    >
    >
    > That variable contains a *string*, not a filehandle...
    >
    >
    >> open UPLOADFILE, ">$upload_dir/$filename";

    >
    >
    > You should always, yes *always*, check the return value from open():
    >
    > open UPLOADFILE, ">$upload_dir/$filename" or
    > die "could not open '$upload_dir/$filename' $!";
    >
    >
    >> while ( <$upload_filehandle> )

    >
    >
    > You need to put an _actual_ filehandle inside the input operator.
    >
    >
    >><HEAD>
    >><TITLE>Thanks!</TITLE>
    >><HEAD>
    >><TITLE>Thanks!</TITLE>
    >></HEAD>

    >
    >
    > That is a rather bizarre document structure...
    >
    >


    I screwed up when copying and pasting from my ssh window. sorry


    >> And here's the log file :
    >> binmode() on closed filehandle UPLOADFILE at

    >
    >
    > Your open() failed, but you didn't know it because you did
    > not check its return value.
    >
    >


    I got it! Thanks!!! for your comments

    > --
    > Tad McClellan SGML consulting
    > Perl programming
    > Fort Worth, Texas
    E Arredondo, Sep 30, 2005
    #9
  10. E Arredondo

    E Arredondo Guest

    "Gunnar Hjalmarsson" <> wrote in message
    news:...
    >E Arredondo wrote:
    >>
    >> use CGI;
    >>
    >> $upload_dir = "/upload";
    >> $query = new CGI;
    >> $filename = $query->param("photo");
    >> $claim = $query->param("claim");
    >> $filename =~ s/.*[\/\\](.*)/$1/;
    >> $upload_filehandle = $query->upload("photo");
    >> open UPLOADFILE, ">$upload_dir/$filename";
    >> binmode UPLOADFILE;
    >> while ( <$upload_filehandle> )
    >> {
    >> print UPLOADFILE;
    >> }
    >> close UPLOADFILE;
    >> print $query->header ( );

    >
    > You may want to use the CPAN module CGI::UploadEasy. The above part of
    > your script can be replaced with:
    >
    > use CGI::UploadEasy;
    > my $ue = CGI::UploadEasy->new(-uploaddir => "/upload");
    > my $cgi = $ue->cgiobject;
    > my $claim = $cgi->param('claim');
    > print $cgi->header;
    >
    > Now, as others have said, I doubt that you have a directory "/upload"
    > under the server root. Maybe you mean "$ENV{DOCUMENT_ROOT}/upload".
    > CGI::UploadEasy includes the necessary checks to find out.
    >
    >


    Thanks for your help, Indeed there was no /upload, I though wrong. Do you
    know if the CPAN CGI:UploadEasy will let me download whole folders ? Or Is
    there a way to do a whole folder download ?

    Thanks
    E Arredondo, Sep 30, 2005
    #10
  11. E Arredondo

    E Arredondo Guest

    <> wrote in message
    news:...
    > You should always check the return value of open(). Try:
    >
    > open(UPLOADFILE, ">$upload_dir/$filename")
    > or die "Can't write to '$upload_dir/$filename': $!\n";
    >
    > I bet the webserver is running as a user that doesn't have permission
    > to write to the specified directory.
    >


    What code should I use it I want the script to email someone about the
    upload ?

    here's the idea :

    print { $filename was uploaded | mail -s $filename henry }

    Thanks?
    E Arredondo, Sep 30, 2005
    #11
  12. Re: I have problems with download scripts, it's trying to print insteadof saving the file

    E Arredondo wrote:
    > What code should I use it I want the script to email someone about the
    > upload ?
    >
    > here's the idea :
    >
    > print { $filename was uploaded | mail -s $filename henry }


    perldoc -q "send mail"

    There are several other modules available; my personal favorite is
    Mail::Sender.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 30, 2005
    #12
  13. Re: I have problems with download scripts, it's trying to print insteadof saving the file

    E Arredondo wrote:
    > Do you know if the CPAN CGI:UploadEasy will let me download whole
    > folders ?


    Suppose you mean upload...

    It sure will let you upload multiple files at once. One way would be to
    include multiple file upload controls in the form, but then you'd need
    to manually pick each file before the submission.

    To _automatically_ have all the files in a folder uploaded, you would
    need to do some pre-processing. It ought to be possible to grab a bunch
    of filepaths and make the file upload request via the LWP family of modules.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 30, 2005
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luis Esteban Valencia
    Replies:
    0
    Views:
    2,508
    Luis Esteban Valencia
    Jan 6, 2005
  2. keto
    Replies:
    0
    Views:
    935
  3. DC
    Replies:
    0
    Views:
    381
  4. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    348
    David Cournapeau
    Dec 30, 2008
  5. Joachim (München)
    Replies:
    5
    Views:
    70
    Dave Burt
    Jun 24, 2006
Loading...

Share This Page