Member Avatar for nblackburn

Hello,

im having a problem with php's file_exists and is_file, it seems to be passing if a file was not specified, i looked into the official php documentation to find file_exist and is_file can also check i directory exists but i dont want this i just want to check if the file exists, any ideas?

<?php
if (file_exists($filepath) == true){
 die('File exists');
} else {
 die('File doesn't exist');
?>

Best Wishes,
Nathaniel Blackburn

Member Avatar for diafol
if (file_exists($filepath)){
 die('File exists');
} else {
 die('File doesn\'t exist');
}
?>
Member Avatar for nblackburn
if (file_exists($filepath)){
 die('File exists');
} else {
 die('File doesn\'t exist');
}
?>

Isn't that kind of what i have already done?

Member Avatar for diafol

'File doesn\'t exist'

you need to backslash the apostrophe.

have you tried it?

Member Avatar for nblackburn

'File doesn\'t exist'

you need to backslash the apostrophe.

This was just a example, to show i was capable of writing this code. The actual code is well over 400 lines. I am led to belive this is required before i can be helped.

Member Avatar for diafol

Perhaps if you posted the actual code that's giving you grief - JUST the relevant bits, not the full 400 lines.

It's impossible to guess why your code isn't working.

if(file_exists($filepath)) {
die('File exists');
} else {
die('File doesn\'t exist');
}

This may be better format comparing the return value as boolean (true). The '$filepath' might be the absolute or relative path including file name and extension.

What's the problem you're facing ? What do you mean that function does not work correctly ? Any error you get, then, post the error message.

If there are no mistakes at the way you have written the only possible reason is to get wrong file location.
Try to echo $filepath , exactly above the code you have written. And check if its the correct location of this file.

Member Avatar for nblackburn
if(file_exists($filepath)) {
die('File exists');
} else {
die('File doesn\'t exist');
}

This may be better format comparing the return value as boolean (true). The '$filepath' might be the absolute or relative path including file name and extension.

What's the problem you're facing ? What do you mean that function does not work correctly ? Any error you get, then, post the error message.

Basically the problem is both file_exists and is_file are returning TRUE when a file hasn't been specified but a directory has.

Member Avatar for diafol

OK check you haven't done:

if(file_exists($file)=true){

Note the single =.

Member Avatar for nblackburn

I have disassembled the code to see whats happening and i think i see. If a file has not been specified it returns the last directory in the filepath.

Basically if the file has NOT been specified or doesn't exist i want it to return false, not the last directory in the path. How could i do this as its driving me mad.

Thanks,
Nathaniel Blackburn

Just make a check with
is_dir($file) it will retunr TRUE if its folder and false if its file

Member Avatar for diafol

ANy chance that you can post your relevant code?

put some more echos in your code, echo out your filename just before you are looking for it, is the path correct? in your 'file_exists' code take it one step further...
and why not use the is_dir() function

//$filename = 'test2.php';   this was a file,
$filename = 'cabron';   this was a directory.

if (file_exists($filename)) {
	echo "$filename EXISTS<br>";	
	if (is_file($filename)) {
		// then the file exists and it is a file
		echo "$filename is a file<br>";
		// you may want to exit and do nothing.				
	} else if (is_dir($filename)) {
		// the file exists and is a directory, not a file...
		// is this expected behavior, if so fine... do nothing
		echo "$filename is a directory<br>";		
	} else {
	    // the filename exists, this code cannot currently be reached, because is_file or is_dir should be true.  fatal error.  die.
		// you could drop the else if is_dir code above , and depending on the is_file() call... this would be the else
		// meaning that the filename exists and it is 'not' a file, then the filename in question would have to be a directory.				
	}	
} else {
	echo "$filename DOES NOT EXIST<br>";
}
Member Avatar for nblackburn

Managed to solve this myself, thanks for the help guys...

<?php
if (is_dir($filepath) == true){
  die('No filename was specified for this download.');
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.