file_exists() returns false, but the file DOES exist
Results of the file_exists()
are cached, so try using clearstatcache()
. If that not helped, recheck names - they might be similar, but not same.
Why is file_exists() returning false when the file exists?
Do a dir on that folder from dos and check that your file is not named
client_secret.json.json
Windows file explorer displays the name of the file without the extension. There for your file name is client_secret.json with the extension of .json
<?php
clearstatcache();
$dir= __DIR__."\client_secret.json.json";
if (file_exists($dir)){
echo "exists";
} else {
echo "doesn't exist at ".$dir;
}
?>
PHP file_exists returns false but the file does exist
Solved, more or less.
To debug I had the idea to move DBFILE to the same folder where the PHP script lives, and check it can find it - it did. Then I move DBFILE one folder after another in the tree to see where it stopped finding it.
It occurs that if only one of the folders in the whole path does not have execute rights for all users (xx5), the file cannot be found and file_exists
returns false
.
So the solution was to create another folder in a totally executable place (/var/www/data/
worked after chmod 755 data
), and move the file there.
Why is file_exists() returning false?
You need to rearrange the way you are telling PHP to look up the address,
$root
is probably not your absolute file path root (probably meaning absolutely) so instead use the special super variable for this, $_SERVER['DOCUMENT_ROOT']
which is the root of the web accessible filepath, therefore you then have:
$img = $_SERVER['DOCUMENT_ROOT'].$root.path.".jpg"
//while retaining your current / at the start of $root
This is the file structure to check if the file exists, not the file structure to reference in the <img>
tag, that appears to be correct in your examples above.
So, your overall correction should look like this:
$root ="/wp-content/uploads/2016/Collection/";
$path = str_replace(" ","_",$uid);
$path = str_replace(".","_",$path);
$path = str_replace(":","",$path);
$img = $root.$path.".jpg";
...
if (file_exists($_SERVER['DOCUMENT_ROOT'].$img)){
....
}
An additional note is that the results of this function are cached so you should call clearstatcache()
at the start of this file so that it can make fresh checks for if the images exist. Currently without this even if the image does exist, PHP will be using the cached - past- results which may not be up to date.
file_exists returns false even when the file exists in my yii php application
The filename $product['image']
is malformed. It doesn't contain file extension, which is why file_exists()
returns false
Related Topics
Root Path Doesn't Work with PHP Include
How to Extract or Uncompress Gzip File Using PHP
How to Iterate Through Dom Elements in PHP
How to Read Xml File from Url Using PHP
Declaring a Global Variable Inside a Function
Time Calculation in PHP (Add 10 Hours)
Laravel: General Error: 1615 Prepared Statement Needs to Be Re-Prepared
Unpacking an Array of Arguments in PHP
How to Use Composer to Autoload Classes from Outside the Vendor
Connecting to Websocket with PHP Client
How to Protect HTML Form from Spammers
How to Replace the Text of a Node Using Domdocument
Save HTML-Formatted Text to Database