CGI upload creates empty file on server?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kristian

    #1

    CGI upload creates empty file on server?

    The script below uploads the selected file and creates it on the
    server in the right directory and all looks fine. But the file is
    empty, it contains nothing...

    HTML FORM:
    <form name="foo" method="post" action="../upload_image.pl "
    target="_self" enctype="multip art/form-data">
    <input type="file" name="file"><in put type="text"
    name="filename" ></form>

    Perl script:

    #!/usr/bin/perl

    use CGI;
    use Cwd;

    #print "Content-type: text/html \n\n";

    #Setup query from cgi
    my $cgi = new CGI;

    #Setup upload directory
    $uploaddir = "/client_image_up loaddir";
    $file = $cgi -> param("file");
    $file =~ s/.*[\/\\](.*)/$1/;
    $filename = $cgi -> param("filename ");
    $datestr = time;

    #Handle the upload
    open FHUP, ">$uploaddi r/$file" or die;
    binmode FHUP;

    undef $Buffer;
    while( read( $file, $Buffer, 1024 ) ){
    print FHUP $Buffer;
    }
    close (FHUP);

    print $cgi -> header();

    print "<h3>Filen '$filename' blev uploaded!</h3>";
    print "<img src=client_imag e_uploaddir/$file alt=$filename> ";
    print "<a href=\"javascri pt:void(history .back());\">Til bage</a> <a
    href=\"javascri pt:void(window. close());\">Luk vindue</a>";

    I also tried without 'buffering' and the result is the same, file is
    created but with no content.

    Any ideas?

    /Kristian
  • nobull@mail.com

    #2
    Re: CGI upload creates empty file on server?

    kristian@detand etfirma.dk (Kristian) wrote in message news:<c9e510f2. 0310202327.2c12 bc80@posting.go ogle.com>...[color=blue]
    > The script below uploads the selected file and creates it on the
    > server in the right directory and all looks fine. But the file is
    > empty, it contains nothing...[/color]

    [color=blue]
    >
    > #!/usr/bin/perl[/color]

    If you had used strict and warnings you would have got errors that
    would have given you a clue as to what was wrong with your program.

    Why do people stubbonly refuse to help themselves?
    [color=blue]
    > $file = $cgi -> param("file");
    > $file =~ s/.*[\/\\](.*)/$1/;[/color]

    The thing returned by param() for an upload field is magically both a
    string and a file handle.

    IMNSHO this was poor design in CGI. Consider using the upload method
    for upload fields.

    Anyhow, once you start to mess with the magic thing as a string, the
    magic is lost and it becomes an ordinary string.

    If you then try to use a the string as a filehandle then you'd usually
    get an error. That's because _usually_ Perl5 scripts have "use
    strict" at the top. If you leave out the "use strict" then (for
    reasons of Perl4 compatability) the string is interpreted as a
    symbolic reference to a file handle.

    I suggest you find the person who advised you not to put "use strict"
    at the top of your scripts, shake them warmly by the throat, and
    explain how not doing so has wasted your time.

    This newsgroup does not exist (see FAQ). Please do not start threads
    here.

    Comment

    Working...