CGI upload creates empty file on server?

Discussion in 'Perl' started by Kristian, Oct 21, 2003.

  1. Kristian

    Kristian Guest

    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="multipart/form-data">
    <input type="file" name="file"><input 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_uploaddir";
    $file = $cgi -> param("file");
    $file =~ s/.*[\/\\](.*)/$1/;
    $filename = $cgi -> param("filename");
    $datestr = time;

    #Handle the upload
    open FHUP, ">$uploaddir/$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_image_uploaddir/$file alt=$filename> ";
    print "<a href=\"javascript:void(history.back());\">Tilbage</a> <a
    href=\"javascript: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
    Kristian, Oct 21, 2003
    #1
    1. Advertising

  2. Kristian

    Guest

    (Kristian) wrote in message news:<>...
    > 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...



    >
    > #!/usr/bin/perl


    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?

    > $file = $cgi -> param("file");
    > $file =~ s/.*[\/\\](.*)/$1/;


    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.
    , Oct 21, 2003
    #2
    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. Replies:
    1
    Views:
    880
    Munna
    Jul 6, 2008
  2. Ronald Fischer
    Replies:
    0
    Views:
    187
    Ronald Fischer
    Jun 5, 2007
  3. Bjoern
    Replies:
    7
    Views:
    317
    Alan J. Flavell
    Oct 14, 2003
  4. rhorizon74
    Replies:
    20
    Views:
    320
    Dr.Ruud
    Feb 1, 2006
  5. TonyV
    Replies:
    19
    Views:
    744
    Mark Clements
    Mar 19, 2007
Loading...

Share This Page