How do I check if a file exists on my hard drive using php? (Linux)

Asked

Viewed 41 times

1

I have a php application that will read return files from the database, for this, I need to know if the file exists on the system hard drive.

I tried it this way: if(file_exists("/caminho/do/arquivo/".$retorno)), the variable $retorno contains the file name with the correct extension. When I run my application through the linux shell (php return.php), it understands that all the files exist, but when I check the folder, it is empty.

What could be going wrong?

1 answer

1


This happens because the function file_exists check if a file or directory exists. And as you said the folder exists but is empty, the function will always return true. To solve this just use the function is_file.

if (is_file("/caminho/do/arquivo/{$retorno}")) {
   // Sua lógica aqui...
}
  • is_file no longer checks if the file exists?

  • You are correct @Andersoncarloswoss, I edited the answer. Vlws!

  • It worked! Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.