File_exists () cannot find file

if(file_exists("./squadra/photos/photog.jpg")) { echo "### YES ###"; } else { echo "### NO ###"; } 

if I run this function on /zones/team.php, it works (it prints YES). If I run this function on /auth/ajax.php, type NO. Why?

EDIT

So, I am doing an experiment.

1 - If I try:

 // file on /zones/team.php if(file_exists($_SERVER['DOCUMENT_ROOT']."/squadra/photos/provag.jpg")) { echo "YES"; } else { echo "NO"; } // file on /auth/ajax.php if(file_exists($_SERVER['DOCUMENT_ROOT']."/squadra/photos/provag.jpg")) { echo "YES"; } else { echo "NO"; } 

he says “NO” to both;

2 - If I try:

 // file on /zones/team.php if(file_exists("./squadra/photos/provag.jpg")) { echo "YES"; } else { echo "NO"; } // file on /auth/ajax.php if(file_exists("../squadra/photos/provag.jpg")) { echo "YES"; } else { echo "NO"; } 

he says YES to both; But on team.php im using./ and on ajax.php .. / ... why does this work ???

+4
source share
7 answers

Your last one works, most likely because:

  • You call zones/team.php from index.php , which is in the root. In this case, the ./ part correctly identifies your current directory.
  • And for ajax, you should call it just like auth/ajax.php , not something like index.php?type=jx&do=auth/ajax , which will be the same as No.1. Therefore, this is not the case, you need to exit auth with ../ , and then continue with squadra/...

Use absolute paths as often as you can. Relative paths are a pain for PHP to compute (in terms of performance).

+3
source

Make sure you are viewing the folder you typed. You start the file address with /, which is the server-side root. If you need a local directory, delete the previous one / or enter the entire path.

Secondly, make sure you don't have typos.

Good luck

+2
source

Once you get the slash, file_exist will go to the root of the hard drive.

Use $ _SERVER ['DOCUMENT_ROOT'] before it or remove the slash and use .. /, etc. etc.

+2
source

If squadra is the directory in the directory where the PHP script runs, try

 if(file_exists('./squadra/photos/photog.jpg')) { echo "### YES ###"; } else { echo "### NO ###"; } 
+1
source

Check php safe_mode status, and check file case sensitivity.

php file_exists

Warning: This function returns FALSE for files inaccessible due to safe mode restrictions. However, these files can be included if they are in safe_mode_include_dir.

+1
source

If you use a relative path with file_exists, it returns false if the path does not belong to the php directory.

0
source

Check the path again - I think the main slash is an error, as it can point to the root (either to the server, or to the more likely user space) - while your executable script may be located in a subpath ...

TL; dg; Try to remove the first "/"

0
source

All Articles