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!


In reply to Check that a value has been assigned to a variable when uploading a photo by Milti

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.