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!

Files Exist

Status
Not open for further replies.

christophermichael

Programmer
Dec 11, 2004
6
US
I am having difficulty with a file exist problem. Below is my code and an explanation of my problem.

I have files in a directory which are the saved as the array @files. I know that this is working fine as I've used it MANY times with success.

Throughout the script each file is refered to as @files[0]. The URL passes along a file number (script.cgi?file=0) so that @files[$file] will return the name of the first file in the directory.

Then I have this piece:
$next = $file + 1;

Meaning that the next file will be the next file in @files.

Here is my exist:

if (-e "./directory/@files[$next]") {
print 'File exists.';
}else{
print 'File does not exist.';
}

Now this keeps returning true no matter what file number I put into the string such as script.cgi?files=10 will return true even if there are only 4 files in the directory.

I did a horrible job of explaining this so please ask for clarification if you need it.

THANKS A TON.

And Merry Christmas Eve =0)
 
@files[$next]

I'm not sure about the rest of your code, and it's chrismtas morning ...

I'd refer to that as $files[$next], rather than @files[$next]

@files[$next] probably returns an empty string, so, the directory at least exists, all the time

SShhh, I think it's santa, he doesn't know I'm still up, don't tell

--Paul

cigless ...
 
Ok I understood you want a true/false statement on the number of files so here you go:

Code:
#!/usr/bin/perl

$files = 10;
$files = $files - 1;

print "Content-type: text/html\n\n";

print <<this;
<html>
<head>
</head>
<body>
this

$dir = "/home/you/public_html/folder";   


			
opendir(DIR, "$dir");
@current = readdir(DIR);
closedir(DIR);
			
foreach(@current)
	{
	push(@currentfiles, $_);
	}
			
if($currentfiles[$files])
	{
	print "There are 10 or more files in the directory";
	}

Remeber 0 counts as 1 so $currentfiles[4] is the fith file. You could modify your if($current[$files]) statement to do a better check, but you get the idea.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top