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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

issue with the existence of the file

Status
Not open for further replies.

imad77

Instructor
Oct 18, 2008
97
CA
Hi,

I try to upload a file form Windows station to a Linux server via a Web application programmed in Perl/CGI.

I use a form to upload a file named c:\pp\tata.txt (it exists in Windows station). But if I change the name of the file to c:\pp\tata1.txt (it does not exist in Windows station), the application uploads the wrong file c:\pp\tata1.txt to the Linux server as aempty file.

I try to avoid this issue by testing if the wrong file exists or not, if it does not exist, it will display an error message "File does not exist". But it does not work fine. I get the error message in both situations, the file exists or does not exist, like this message:
=================================
File c:\pp\tata.txt does not exist
=================================

What is it wrong with my script? Thanks

Here is a prt of the script:
=============
$fpath=$q->param('upfile');

if ( ! -e "$fpath"){

print "File $fpath does not exist";

return;
}
 
contains the name and the path of the file located in windows and its result is like : c:\pp\tata1.txt
 
You must use the full path to the file or be in the directory where the file is if you only want to use the file name with the -e operator.

Code:
if (! -e "full/path/to/file") {
   ....
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
But I tried to check if the file exists in Windows station not in Lunux server.

I try to check if c:\pp\tata1.txt exists in Windows station that I try to upload from this command:

print $q->filefield(-name=>'upfile',-size=>30,-maxlength=>80);

 
You can't check if the file exists on a different computer using -e (if that is what you are thinking/doing).

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
is a way to avoid to upload an non-existing file as empty file?
 
Or is it a way to prevent people to chnage the name of file in the form? and force them to browse the system and select it.

print $q->filefield(-name=>'upfile',-size=>30,-maxlength=>80);
 
Oh, I found the solution:

I have to use that:

if (<$fpath>){....

If the file is readeable, the script can continue, if not, it stops.

thanks Guys for your great help
 
Or is it a way to prevent people to chnage the name of file in the form? and force them to browse the system and select it.

That is always the case. The only way to enter a filename in a file field is to use the browse button. Any value entered otherwise is ignored.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Oh, I found the solution:

I have to use that:

if (!<$fpath>){....

If the file is not readeable, the script stops.

thanks Guys for your great help
 
That code seems a lil off. $fpath is the file path, e.g. "C:\whatever\file.txt"? Then treating it like a filehandle by doing <$fpath> shouldn't work.

It should be safe enough to assume that your user won't legitimately upload an empty file. So just check if you get no data.

Code:
my $fpath = $q->param("file"); # get file path/name
my $fhandle = $q->upload("file"); # get file handle for upload

# read file handle
my $bin = '';
while (<$fhandle>) {
   $bin .= $_;
}

# empty file?
if (length $bin == 0) {
   print "No file data was read; file $fpath probably doesn't exist";
}

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top