If your filename is empty, then you are passing only the directory name to file_exists
. Directories are files too. And I reckon the directory does actually exist. It is not lying to you.
You can either check that the filename from the database is not empty, or you can pass the whole string to the function is_dir
to see if it is a directory. I assume you only want regular files.
That would look something like this:
<?php
$file = $row['attach'];
$exists = file_exists('uploads/'.$file.'') && !is_dir('uploads/'.$file.'');
if ($exists) {
echo $file;
} else {
echo 'No file1';
}
?>
I changed the if
statement to use an else
clause. It is equivalent to using a second if
like you did.