简体   繁体   中英

file_exists() isn't finding the 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 print YES). If i run this function on /auth/ajax.php it print NO. Why?

EDIT

So i make some 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";              
}    

it says NO on 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";              
}

it says YES on both; But on team.php im using ./ and on ajax.php ../ ...why this works???

Your last one works most probably because:

  1. You are calling zones/team.php from an index.php which resides in root. In this case ./ part correctly identifies your current directory.
  2. And for ajax, you must be calling it directly like auth/ajax.php , instead of something like index.php?type=jx&do=auth/ajax which would be the same as No.1. Hence this is not the case, you need to get out of auth first with ../ , and then go on with squadra/... .

Use absolute paths as often as you can. Relative paths are a pain for PHP to calculate them (in performance-wise).

Make sure you are considering the folder that you have typed. You start the file address with / which is server side root. If you want local directory, either remove the preceeding / or type out the entire path.

Secondly make sure you have no typos.

Good luck!

As you got a forward slash, file_exist will go to the root of the HDD.

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

If squadra is a directory under the directory where the PHP script is running, try

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

Check the php safe_mode status, and check the case sensitivity of the file path.

php file_exists

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

如果对file_exists使用相对路径,则返回false,除非该路径是相对于php目录的。

Check for the path again - I think that the leading slash is a mistake - as it may point to the root (either server or more likely user space) - while your executing script may be located in a sub-path...

tl;dr; Try removing the first "/"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM