help with upload perl

Discussion in 'Perl Misc' started by News sender, Apr 20, 2004.

  1. News sender

    News sender Guest

    Hi there,

    I am using CGI and Perl for uploading files. The Perl code is included.

    The code works fine on PC and Linux (both run apache 2.0.39). But when I run
    the code on Unix (solaris7, also run Apache2.0.39), only the filename is
    uploaded, not the contents:

    -rw------- 1 nobody nobody 0 Apr 19 16:10 file1
    -rw------- 1 nobody nobody 0 Apr 19 16:35 file2

    You can see the file names are right: file1, file2. But the size of each
    file is 0.

    The permission of the directory is set as 777:
    drwxrwxrwx 2 nobody nobody 512 Apr 2 15:33 shared

    There is no error messages in the Apache log files.

    Does anyone see this problem before? Any help is appreciated.

    Many thanks.

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

    use CGI;

    $query = new CGI;

    $filepath = $query->param ('filename');

    if ($filepath =~ /([^\/\\]+)$/)
    {
    $filename="$1";
    print "The file name is: ", $filename, "<br>";
    }

    else
    {
    $filename="$filepath";
    }

    # check the existence of the file
    if (-e "../htdocs/sharedfiles/$filename")
    {
    print "A file with the same name exists, please rename your file.",
    "<br>";
    }
    else
    {
    # open the file and load it
    open (OUTFILE,">../htdocs/sharedfiles/$filename ");
    binmode(OUTFILE);
    while ($bytesread=read($filepath,$buffer,1024)) {
    print OUTFILE $buffer;
    }
    close OUTFILE;
    print "Your file has been successfully uploaded.", "<br>";
    }
     
    News sender, Apr 20, 2004
    #1
    1. Advertising

  2. News sender

    Clyde Ingram Guest

    Dear renping.liu,

    "News sender" <> wrote in message
    news:c62rts$krj$...

    > The code works fine on PC and Linux (both run apache 2.0.39). But when I

    run
    > the code on Unix (solaris7, also run Apache2.0.39), only the filename is
    > uploaded, not the contents:
    >
    > -rw------- 1 nobody nobody 0 Apr 19 16:10 file1
    > -rw------- 1 nobody nobody 0 Apr 19 16:35 file2
    >
    > You can see the file names are right: file1, file2. But the size of each
    > file is 0.


    Have a look at http://stein.cshl.org/WWW/software/CGI/
    Lincoln starts the section "USING the File Upload Feature" by warning:

    "The file upload feature doesn't work with every combination of browser and
    server. The various versions of Netscape and Internet Explorer on the
    Macintosh, Unix and Windows platforms don't all seem to implement file
    uploading in exactly the same way. I've tried to make CGI.pm work with all
    versions on all platforms, but I keep getting reports from people of
    instances that break the file upload feature. "

    In the "Frequent Problems" sub-section, look at the problem entitled "You
    can read the name of the uploaded file, but can't retrieve the data":

    "First check that you've told CGI.pm to use the new multipart/form-data
    scheme. If it still isn't working, there may be a problem with the temporary
    files that CGI.pm needs to create in order to read in the (potentially very
    large) uploaded files. Internally, CGI.pm tries to create temporary files
    with names similar to CGITemp123456 in a temporary directory. To find a
    suitable directory it first looks for /usr/tmp and then for /tmp. If it
    can't find either of these directories, it tries for the current directory,
    which is usually the same directory that the script resides in."

    In the section "Creating a File Upload field", Lincoln says:

    "In order to take full advantage of the file upload facility you must use
    the new multipart form encoding scheme. You can do this either by calling
    startform() and specify an encoding type of $CGI::MULTIPART or by using the
    new start_multipart_form() method. If you don't use multipart encoding, then
    you'll be able to retreive the name of the file selected by the remote user,
    but you won't be able to access its contents."

    He offers a very detailed analysis of problems and "best practice", in
    particular:
    1. "use strict;"
    You forgot to put this at the top of your program. You also forgot to
    enable warnings:
    #!/usr/bin/perl -w
    use strict;

    2. "To be safe, use the upload() function"

    So, instead of doing this:

    > while ($bytesread=read($filepath,$buffer,1024)) {


    Try something like this:

    1. Form Generation phase:

    print
    header(),
    start_html(),
    start_multipart_form( -name => 'myform' ),
    filefield( -name => 'filename', 'maxlength' => 64, override => '1' ),
    submit( -name 'Upload file', 'value' =. 'upload' ),
    end_form(),
    end_html();

    2. Handling Form submission:

    my $fh = upload( 'filename' );
    while ( read $fh, $buffer, 1024 ) {
    print OUTFILE, $buffer;
    }

    I have used code like this in a CGI script on Solaris 2.6, with Perl 5.004
    (!!) and CGI.pm 2.76 with no problems.
    (I was uploading text files, so did not need binmode.)

    I hope this helps.
    Regards,

    Clyde
     
    Clyde Ingram, Apr 20, 2004
    #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. dpackwood
    Replies:
    3
    Views:
    1,863
  2. =?Utf-8?B?U2FyYXY=?=

    Upload a file without file Upload control - ASP.Net

    =?Utf-8?B?U2FyYXY=?=, Aug 2, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,584
    Bruce Barker
    Aug 3, 2005
  3. Heather Fraser
    Replies:
    0
    Views:
    665
    Heather Fraser
    Jul 5, 2004
  4. Replies:
    1
    Views:
    969
    Gabriel Genellina
    Nov 25, 2006
  5. Prakash
    Replies:
    3
    Views:
    492
    Ray at
    Nov 12, 2003
Loading...

Share This Page