PHP clarifation of file_exists function, does it check for the actual file or just a path

StackOverflow https://stackoverflow.com/questions/23143749

Вопрос

I have a list of txt file names and paths in a SQL db. I have a directory that contains only a few files (a lot of the links are bad links). I'm using:

$filename = '/path/somename.txt';

if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}

to check for the file, but I'm noticing that it's always coming back as if the file is there. Is there a better alternative for this function for what I'm trying to do, or am I doing something else wrong. Thank you in advance!

Это было полезно?

Решение

file_exists should validate that the file exists, not just that the path to the file exists.

If you're having problems you might try including the document root in your path.

Другие советы

try this :

$filename = dirname(__FILE__)."/path/somename.txt";

 echo  file_exists($filename) ? "The file $filename exists" : "The file $filename does not exist";
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top