lmck2 has asked for the wisdom of the Perl Monks concerning the following question:

I have had a script running for a couple of years with no problems. It uploads a file. A couple of weeks ago it quit saving the files, everything else in the script continued to work.

I built this short program to see if I could get it to work? It doesn't work either. It is running on Apache version 2.2.17. They recently upgraded to cpanel 11. They assure me they haven't done anything to affect my script. :)

#!/usr/bin/perl -- use CGI qw( :standard escapeHTML ); my $directory = "/home/curbdire/www/uploadedimage/"; my $query = new CGI; my @names = $query->param; my $url = $query->param("URL"); my $fh = $query->upload('upload_file'); my $filename = $query->param('name'); $filename =~ s/[^A-Za-z0-9\.\_]/_/g; print "Content-type: text/html\n\n"; open(OUTF, "$directory$filename") || Error; while ( $bytesread = read $fh, $buffer, 1024 ) { print OUTF $buffer; } close OUTF; print "$directory$filename"; Sub Error{ print " error - $! "; }

Output is as follows:

/home/curbdire/www/uploadedimage/test.gif error - Bad file descriptor

Anybody have any thoughts or ideas - I am totally stumped and at a loss to where to go next. Thank you for your assistance

Replies are listed 'Best First'.
Re: Problems Uploadng a file
by zentara (Cardinal) on Sep 13, 2011 at 15:55 UTC
    Check that the file permissions are world writable on your upload directory, and even if the directory still exists. Finally check your httpd logs to see what error is logged.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
        Permission on folder "uploadedimage" is set to 777.

        Dangerous in a shared hosting environment.

        My http logs create only the following errors:
        Tue Sep 13 08:52:50 2011 error client 75.169.101.214 File does not exi +st: /home/curbdire/public_html/404.shtml Tue Sep 13 08:52:50 2011 error client 75.169.101.214 File does not exi +st: /home/curbdire/public_html/favicon.ico

        The first one comes from a missing 404 handler, configured somewhere in the web server configuration file. Ignore it, or treat it like a generic 404 error. The second one is an automatic request for a web site icon, originally invented by Microsoft. Ignore it or upload some icon to the root directory of the website.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Problems Uploadng a file
by Marshall (Canon) on Sep 13, 2011 at 17:02 UTC
    It appears to be that you are opening OUTF for read which will not exist, so you get "test.gif" - Bad file descriptor error.
    open(OUTF, "$directory$filename") || Error; ...should be... open(OUTF, '>', "$directory$filename") || Error;
      I made the change. I am still getting the error - no change.
Re: Problems Uploadng a file
by duyet (Friar) on Sep 13, 2011 at 19:39 UTC
    You might want to check on $fh before doing something with it
    my $fh = $query->upload('upload_file'); if ( defined $fh ) { # do your stuff }
      The $fh doesn't seem to be where the problem is. I put a
      while ( $bytesread = read $fh, $buffer, 1024 ) { print "$buffer"; print OUTF $buffer; } close OUTF;
      and it filled the screen with the stuff you would expect from a binary file. The problems seems to be in line
      open(OUTF, '>', "$directory$filename") || Error;
      It seems that for some reason it does not like the way I am describing or naming the file location.
        read() is a lower level routine and it works with binary. You are just copying raw bits from $fh to OUTF.

        set $fh and OUTF to binary mode by putting this: binmode($fh); binmode(OUTF); before the while statement.

        binmode() a Perl function - read more here: binmode.

        This does not do anything on open() failure:

        open(OUTF, '>', "$directory$filename") || Error;

        This does not define a subroutine:

        Sub Error{ print " error - $! "; }

        This would have helped:

        use warnings; use strict;
Re: Problems Uploadng a file
by Khen1950fx (Canon) on Sep 14, 2011 at 11:52 UTC
    Try this. I think that you'll see what I did differently.
    #!/usr/bin/perl -slw use strict; use CGI qw( :standard escapeHTML ); my $query = new CGI; my $directory = '/root'; my @names = $query->param; my $url = $query->param("URL"); my $upload = $query->upload('upload_file'); my $name = $query->param('name'); my $file = 'tick.html'; print "Content-type: text/html"; binmode STDOUT, ":encoding(utf8)"; open my $fh, '<', "$directory/$file" || die $!; my $buffer; my $bytesread = read( $fh, $buffer, 80 ); print "$directory/$file"; close $fh;

      Thank you Monks for your assistance

      I got my simple script to work, now I have to look at the original script. Thanks for your assistance

Re: Problems Uploadng a file
by Anonymous Monk on Sep 13, 2011 at 19:15 UTC
Re: Problems Uploadng a file
by afoken (Chancellor) on Sep 14, 2011 at 11:01 UTC

    No strict, no warnings, no taint mode, and still you want to allow arbitary people to upload arbitary data to your web server.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)