Trickster of the rascal 1:
The function file_exists()
, despite the name, checks if the path exists, regardless of whether it is a file or directory.
When you leave the parameter empty, your test is the same as this:
if (file_exists( '/pasta/' ))
what should return true if the folder actually exists.
Solution:
The solution would be to exchange the function for is_file()
, which is specific to files:
if (is_file($pastadestino))
{
echo "OK";
}
else
{
echo "NOK";
}
If you want to check folders/directories only, there is the analog function is_dir()
, that check if the path exists and is directory.
if (is_dir($pastadestino))
{
echo "OK";
}
else
{
echo "NOK";
}
Trickster of the rascal 2:
Be careful if you need to scan files right after changing something in the filesystem, as all of the above functions have a cache in PHP.
Solution:
To make sure you are dealing with the updated path, there is the clearstatcache()
:
clearstatcache(); // Limpamos o cache de arquivos do PHP
if (is_file($pastadestino)) // is_dir() para pastas, is_file para arquivos.
{
echo "OK";
}
else
{
echo "NOK";
}
Click on the names of the above functions to know more details, they are linked with online PHP documentation.
It seems all right to me... here it is
NOK
, see on ideone.– gustavox