Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Check that a value has been assigned to a variable when uploading a photo

by Milti (Beadle)
on Jan 14, 2014 at 18:33 UTC ( [id://1070581]=perlquestion: print w/replies, xml ) Need Help??

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

I am using the form shown below to upload photos to a webserver via a cgi program.

<html> <body> <FORM ENCTYPE="multipart/form-data" ACTION="http://www.diversitylink.com/cgi-bin/file-upload.pl" METHOD="POST">

Please select a file to upload:

<INPUT TYPE="FILE" NAME="photo" size="20">

Please enter your account ID:

<input type="text" Name="AccountID" size="30">

<INPUT TYPE="submit" NAME="Submit" VALUE=Submit Form">

I am using a cgi program that I got from a tutorial on the web which is unchanged except for the addition of "$AccountID". Here's the program:

#!/usr/bin/perl -w use strict; use CGI; use CGI::Carp qw ( fatalsToBrowser ); use File::Basename; $CGI::POST_MAX = 1024 * 5000; my $safe_filename_characters = "a-zA-Z0-9_.-"; my $upload_dir = 'E:/root/mywebsite/htdocs/somedirectory/someotherdire +ctory'; my $query = new CGI; my $filename = $query->param("photo"); if ( !$filename ) { print $query->header ( ); print "There was a problem uploading your photo (try a smaller file). +"; exit; } my $AccountID = $query->param("AccountID"); my $path = $filename; my $base = basename($path); my $dir = dirname($path); my $file = basename($path); my ( $base, $dir, $extension ) = fileparse ( $path, '\..*' ); ##$path = shift; my $ext = (fileparse ($path, '\..*?')) [2]; $ext=~ s/^\.//; $filename = $AccountID . '.' . $ext; my $upload_filehandle = $query->upload("photo"); open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!"; binmode UPLOADFILE; while ( <$upload_filehandle> ) { print UPLOADFILE; } close UPLOADFILE; print $query->header ( ); print "Thanks for uploading your photo."; exit;

The line "if ( !$filename )" seems to check the size of the uploaded file and apparently works well. However I have not been able to write an if statement that checks to see if the variable $filename actually contains any information. I've tried things such as if ($filename eq "") but the program ignores that statement when no file has been selected for uploading.

Can someone tell me the correct way to check to see if the variable $filename is empty?

Any assistance will be much appreciated!

  • Comment on Check that a value has been assigned to a variable when uploading a photo
  • Download Code

Replies are listed 'Best First'.
Re: Check that a value has been assigned to a variable when uploading a photo
by tangent (Parson) on Jan 14, 2014 at 20:54 UTC
    The line "if ( !$filename )" seems to check the size of the uploaded file and apparently works well.
    That's not really what's happening there. If the size of the file being uploaded is greater than $CGI::POST_MAX then CGI will abort the process altogether and all params will be empty, so $filename will be empty. If no file has been selected for uploading, $filename will be empty then also.

    if ( !$filename ) does perform the test you require - it is saying "if $filename is empty" (or rather, if $filename does not contain a true value) then return the error message.

    You can check if an attempt to upload too big a file was made by using the cgi_error() function:

    my $error = $q->cgi_error; if ($error) { # return an error message }
    $error would contain the message "413 Request entity too large"

    Also, you are not checking if $AccountID has a value - you can check that the same way: if ( !$AccountID ) { # return an error message }

Re: Check that a value has been assigned to a variable when uploading a photo
by Anonymous Monk on Jan 14, 2014 at 20:57 UTC
Re: Check that a value has been assigned to a variable when uploading a photo
by PerlSufi (Friar) on Jan 14, 2014 at 19:33 UTC
    Hello. I would first try to use Data::Dumper to print the contents of $filename and see if it is doing what you think it is. you could also try  if (length($filename) == 0) {..do stuff..;}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1070581]
Approved by lidden
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-09-08 21:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.