file_exists returns False even if the file exists

Asked

Viewed 211 times

-1

In the system I’m using the file_exists to see if the image exists in the directory, the problem is that it always returns false even though the image exists in the directory, I’ve done almost all possible tests and even so returns false.

Code:

<?php    
  $Image = "http://megaki/uploads/windows/173/1731534093656.jpeg";

if (file_exists($Image)) {
echo "O arquivo $Image existe";
} else {
echo "O arquivo $Image não existe";
}
?>

When executing returns the message:

O arquivo http://megaki/uploads/windows/173/1731534093656.jpeg não existe

The directory is correct when creating the directory give permission 077 and yet I can’t fix it I’m already 2 days trying to fix it and I can’t.

inserir a descrição da imagem aqui

  • use file_get_contents then save the content to a local file or check directly with fopen

1 answer

1

It is usually used file_exists to test the existence of a physical file, not an http address.

In your case, it seems to me that you are trying to check whether a file that is in a directory of your project exists or not. In this case, use the physical file path on your server to check.

Thus:

define('ROOT_DIR', 'diretório/raiz/do/projeto');

$file = ROOT_DIR . '/uploads/windows/173/1731534093656.jpeg';

var_dump(file_exists($file));

Tip

To find the default project folder automatically, I recommend using the variable __DIR__.

In my applications, I usually only use the index.php, so I don’t usually have problems with this constant varying the value by using a script in another folder, for example.

  • tried it all the way and still didn’t give think tever be some kind of folder permission

Browser other questions tagged

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