Uploaded File Empty but in correct folder with correct name

Discussion in 'Perl Misc' started by froil, Feb 28, 2006.

  1. froil

    froil Guest

    When i run the script below the output looks good, but the file that is
    uploaded is 0 bytes. If anyone can help me find out why the file is
    empty that would be awesome.

    the script is passed the values of
    filename and description through a web form
    www.jilesfamily.net/uploadtest.html using the encoding of
    multipart/formdata.

    #!/usr/bin/perl -wT
    #uploadtest.cgi
    use strict;
    use CGI qw:)standard);
    use CGI::Carp qw(fatalsToBrowser);
    $CGI::pOST_MAX = 1024 * 1000;
    my $upload_dir =
    "/hsphere/local/home/jilesfre/jilesfamily.net/uploads";
    my $File_Name = param('filename');
    if (!$File_Name && cgi_error()) {
    print header(-status=>cgi_error());
    exit 0;
    }
    my $description = param('description');
    my $New_Name = Get_Full_Name($File_Name);
    Store_Results();
    Print_Results();
    sub Print_Results{
    print header;
    print start_html('TEST Pictures');
    print qq(<h1>Description: $description</h1>);
    print qq(<h1> File Name: $File_Name</h1>);
    print qq(<h1> New Name: $New_Name</h1>);
    print end_html;
    }
    sub Store_Results{
    my $data;
    open (STORAGE, ">$upload_dir/$New_Name")
    or die "Error: $upload_dir/$New_Name: $!\n";
    binmode ($File_Name);
    binmode (STORAGE);
    while( read($File_Name, $data, 1024) ){
    print STORAGE $data;
    }
    close STORAGE;
    }
    sub Get_Full_Name{
    my $full_name = shift;
    $full_name =~ s/.*[\/\\](.*)/$1/;
    return($full_name);
    }
     
    froil, Feb 28, 2006
    #1
    1. Advertising

  2. froil

    Samwyse Guest

    froil wrote:
    > When i run the script below the output looks good, but the file that is
    > uploaded is 0 bytes. If anyone can help me find out why the file is
    > empty that would be awesome.


    > my $File_Name = param('filename');

    [...]
    > binmode ($File_Name);

    [...]
    > while( read($File_Name, $data, 1024) ){


    'binmode' and 'read' both want file handles as their only/first
    parameter; what you have is the name of a file. I don't remember
    exactly how CGI handles files, but you may want to do something similar
    to this:
    open FH, $File_Name or die;
    binmode FH;
    while (read FH, $data, 1024) {
     
    Samwyse, Feb 28, 2006
    #2
    1. Advertising

  3. Samwyse wrote:
    > froil wrote:
    >> When i run the script below the output looks good, but the file that is
    >> uploaded is 0 bytes. If anyone can help me find out why the file is
    >> empty that would be awesome.
    >>
    >> my $File_Name = param('filename');
    >> [...]
    >> binmode ($File_Name);
    >> [...]
    >> while( read($File_Name, $data, 1024) ){

    >
    > 'binmode' and 'read' both want file handles as their only/first
    > parameter; what you have is the name of a file.


    From "perldoc CGI":
    "When the form is processed, you can retrieve the entered filename by
    calling param():

    $filename = $query->param('uploaded_file');
    ....
    The filename returned is also a file handle. You can read the contents
    of the file using standard Perl file reading calls:"

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 28, 2006
    #3
  4. Gunnar Hjalmarsson, Feb 28, 2006
    #4
  5. froil

    Samwyse Guest

    Gunnar Hjalmarsson wrote:
    > Samwyse wrote:


    >> 'binmode' and 'read' both want file handles as their only/first
    >> parameter; what you have is the name of a file.

    >
    > From "perldoc CGI":
    > "When the form is processed, you can retrieve the entered filename by
    > calling param():
    >
    > $filename = $query->param('uploaded_file');
    > ...
    > The filename returned is also a file handle. You can read the contents
    > of the file using standard Perl file reading calls:"


    Well, I *did* say that it had been a while since I did any CGI work.

    Next time a CGI question comes in, I'll just keep my mouth shut.
     
    Samwyse, Feb 28, 2006
    #5
  6. froil

    froil Guest

    ok i used uploadeasy and it gave me the error Can't use an undefined
    value as a HASH reference at
    /usr/lib/perl5/site_perl/5.6.1/CGI/UploadEasy.pm line 209
    here is the code... please help
    use strict;
    use CGI qw:)standard);
    use CGI::Carp qw(fatalsToBrowser);
    use CGI::UploadEasy;
    $CGI::pOST_MAX = 1024 * 1000;
    my $query = new CGI;
    my $upload_dir =
    "/hsphere/local/home/jilesfre/jilesfamily.net/uploads";
    my $File_Name = $query->param('filename');
    my $description = param('description');
    my $ue = CGI::UploadEasy->new(-uploaddir => $upload_dir);
    my $Url_Path = "uploads";
    my $File_Name = param('filename');
    my $Description = param('description');
    my $File = ( keys %{ $ue->fileinfo } )[0];
    Print_Results();
    sub Print_Results{
    print header;
    print start_html('TEST Pictures');
    print qq(<h1>Description: $description</h1>);
    print qq(<h1> File Name: $File_Name</h1>);
    print end_html;
    }
     
    froil, Mar 1, 2006
    #6
  7. froil

    DJ Stunks Guest

    froil wrote:
    > ok i used uploadeasy and it gave me the error Can't use an undefined
    > value as a HASH reference at
    > /usr/lib/perl5/site_perl/5.6.1/CGI/UploadEasy.pm line 209
    > here is the code... please help
    > use strict;
    > use CGI qw:)standard);
    > use CGI::Carp qw(fatalsToBrowser);
    > use CGI::UploadEasy;
    > $CGI::pOST_MAX = 1024 * 1000;
    > my $query = new CGI;
    > my $upload_dir =
    > "/hsphere/local/home/jilesfre/jilesfamily.net/uploads";
    > my $File_Name = $query->param('filename');
    > my $description = param('description');
    > my $ue = CGI::UploadEasy->new(-uploaddir => $upload_dir);
    > my $Url_Path = "uploads";
    > my $File_Name = param('filename');
    > my $Description = param('description');
    > my $File = ( keys %{ $ue->fileinfo } )[0];
    > Print_Results();
    > sub Print_Results{
    > print header;
    > print start_html('TEST Pictures');
    > print qq(<h1>Description: $description</h1>);
    > print qq(<h1> File Name: $File_Name</h1>);
    > print end_html;
    > }


    bleagh.

    operator, could you please connect me to perldoc perlstyle?

    -jp
     
    DJ Stunks, Mar 1, 2006
    #7
  8. froil wrote:
    > ok i used uploadeasy and it gave me the error Can't use an undefined
    > value as a HASH reference at
    > /usr/lib/perl5/site_perl/5.6.1/CGI/UploadEasy.pm line 209


    It's because you both create a CGI object and call CGI functions before
    the CGI::UploadEasy object has been created. See the CAVEATS section in
    the CGI::UploadEasy POD.

    > $CGI::pOST_MAX = 1024 * 1000;


    That line is redundant, since $CGI::pOST_MAX is set by CGI::UploadEasy.

    > my $query = new CGI;


    That line creates a CGI object, and causes your script to fail later.
    Remove it.

    > my $File_Name = $query->param('filename');
    > my $description = param('description');
    > my $ue = CGI::UploadEasy->new(-uploaddir => $upload_dir);


    Wrong order. And since you are using CGI.pm's function-oriented style in
    the rest of your script, why not do so here as well?

    my $ue = CGI::UploadEasy->new(-uploaddir => $upload_dir);
    my $File_Name = param('filename');
    my $description = param('description');

    Optionally, if you want to use CGI.pm's object-oriented style, the best
    way is to do:

    my $query = $ue->cgiobject;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Mar 1, 2006
    #8
  9. froil

    froil Guest

    ok i changed the code to what it is below and i am still getting the
    same can't use a value as undef hash ref line 209. I really appriciate
    all the help you guys have given me.
    #!/usr/bin/perl -w
    #uploadtest.cgi
    use strict;use
    CGI::UploadEasy;
    use CGI qw:)standard);
    use CGI::Carp qw(fatalsToBrowser);
    use Data::Dumper;
    my $upload_dir =
    "/hsphere/local/home/jilesfre/jilesfamily.net/uploads";
    my $Description = param('description');
    my $ue = CGI::UploadEasy->new(-uploaddir => $upload_dir);
    my $Url_Path = "cgi-bin/pictures";
    my $File_Name = param('filename');
    my $info = $ue->fileinfo;
    print header('text/plain');
    print Dumper $info;
     
    froil, Mar 1, 2006
    #9
  10. froil wrote:
    > ok i changed the code to what it is below and i am still getting the
    > same can't use a value as undef hash ref line 209.


    That's because you still call the CGI::param() function before the
    creation of the CGI::UploadEasy object.

    > my $Description = param('description');
    > my $ue = CGI::UploadEasy->new(-uploaddir => $upload_dir);


    Switch the order of those two lines.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Mar 1, 2006
    #10
  11. froil

    froil Guest

    Thanks Gunner it is working so far!!! You are great.
     
    froil, Mar 2, 2006
    #11
  12. froil

    froil Guest

    Ok so now i add a little bit to connect to my database to store the
    description, and i get error 500. when i run the syntax check i get no
    errors and all the error log says is premature end of script. i can't
    find any stupid mistakes, but i am sure there is one.

    #!/usr/bin/perl -wT
    #uploadtest.cgi
    use strict;
    use CGI qw:)standard);
    use DBI;
    use CGI::Carp qw(fatalsToBrowser);
    use CGI::UploadEasy;
    my $upload_dir =
    "/hsphere/local/home/jilesfre/jilesfamily.net/uploads";
    my $ue = CGI::UploadEasy->new(-uploaddir => $upload_dir);
    my $Url_Path = "cgi-bin/pictures";
    my $File_Name = param('filename');
    my $info = $ue->fileinfo;
    my $Description = param('description');
    my $File = ( keys %{ $ue->fileinfo } )[0];
    my $username = '?';
    my $password = '?';
    my $data_source = 'DBI:mysql:jilesfr_pictures@69.6.255.192:3306';
    my $DBH = DBI->connect( $data_source, $username, $password )
    or die "Error: $DBI::errstr\n";
    my $sth_insert =
    $DBH->prepare( qq{INSERT INTO files (Description,
    FileName) VALUES (?,?)} )
    or die "Error: can't connect! $DBH::errstr\n";
    $sth_insert->execute($Description,$File);
    $DBH->disconnect;
    print header;
    print start_html('files uploaded');
    print qq(<h1>File: $File</h1>);
    print qq(<h2>Description: $Description</h2>);
     
    froil, Mar 2, 2006
    #12
  13. froil wrote:
    > Ok so now i add a little bit to connect to my database to store the
    > description, and i get error 500. when i run the syntax check i get no
    > errors and all the error log says is premature end of script.


    Sounds like you are on a Windows box and forgot to upload to *nix in
    ASCII transfer mode.

    Otherwise, please see

    perldoc -q 500

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Mar 2, 2006
    #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. Matt G
    Replies:
    1
    Views:
    1,201
    Deepak Kumar Vasudevan
    Aug 22, 2003
  2. =?Utf-8?B?YXNhZA==?=

    getting uploaded file name

    =?Utf-8?B?YXNhZA==?=, Apr 18, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    490
    =?Utf-8?B?TWFuc28=?=
    Apr 18, 2005
  3. bradley
    Replies:
    1
    Views:
    1,221
    Peter Rilling
    Jun 8, 2005
  4. =?Utf-8?B?dGhlIGZyaWVuZGx5IGRpc3BsYXkgbmFtZQ==?=

    Elegant way to get name of uploaded file?

    =?Utf-8?B?dGhlIGZyaWVuZGx5IGRpc3BsYXkgbmFtZQ==?=, Oct 11, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    469
    Peter Rilling
    Oct 11, 2005
  5. AA
    Replies:
    2
    Views:
    436
    Andrea Desole
    Jun 13, 2006
Loading...

Share This Page