Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

upload files to a directory

Status
Not open for further replies.

there

Programmer
Feb 4, 2002
15
CA
I have written a code to upload files to a directory but it only works if something is place in the upload files field. The problem arises when the upload file field is empty and you make changes to some of the other fields on the same form and it will not work.

Where am I going wrong? How can I tell cgi that when the upload file field is empty, this is okay and go ahead and process the form in the database with the changes from other fields.

the code
-------
##--------
# uploadImage()
##--------
sub uploadImage
{

# need this transfer the right size of the file
$upload_logo = param('logo1');
$logo1 = lc(join '', split / /,(split/\\/, param("logo1"))[-1]);
# $upfile = lc(join '', split / /,(split/\\/, $logo1)[-1]);

$abs_file_to_create .= "/locato_on_web_server/";
$abs_file_to_create .= "$logo1";

open(NEW_FILE,">$abs_file_to_create") || die "Can't open $abs_file_to_create for output: $!";
binmode NEW_FILE;
while (read($upload_logo, $data, 8192))
{
print NEW_FILE $data;
}
close NEW_FILE;

}
Thank you for your knowledge, wisdom and time!

There
 
First step is to break up you code into small bits like it seems you have using subroutines. Next, You can test to see if the upload parameter exists in several ways. I will demonstrate three.

$upload_logo = param('logo1');
if ($upload_logo){#<--This test if it exists
....do the upload routine
} else {
....fail and go on to something else
}

if (!($upload_logo)){#<--This test if it does not exists
....do somethine else
} else {
....do upload
}

Lastly and shortest
$upload_logo = param('logo1');
$upload_logo || sometothersub();#<--again testing if doesnot exist

I will explain this one. The variable $upload_logo fails if it is empty and the '||' means 'OR' and allows you to redirect to and errorRoutine, die, or another location in your script or set the varibable to safe known value.

One last note, if you test $upload_logo within your subroutine then you can use the 'return;' command to safely exit the subroutine and continue on with the rest of the program.

Try this or some variation

$upload_logo = param('logo1');
$uploadstatusmsg=uploadImage($upload_logo);
print &quot;$uploadstatusmsg&quot;;
##--------
sub uploadImage{
my $upload_logo=@_;
if (!($upload_logo)){return 'No Upload file submintted!';}
my $logo1 = lc(join '', split / /,(split/\\/, $upload_logo)[-1]);
# $upfile = lc(join '', split / /,(split/\\/, $logo1)[-1]);

my $abs_file_to_create = &quot;/locato_on_web_server/$logo1&quot;;


open(NEW_FILE,&quot;>$abs_file_to_create&quot;) || die &quot;Can't open $abs_file_to_create for output: $!&quot;;
binmode NEW_FILE;
while (read($upload_logo, $data, 8192))
{
print NEW_FILE $data;
}
close NEW_FILE;
return 'Successful Upload';
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top