简体   繁体   中英

PHP file_exists will not work

I have been having problems with file_exists function in PHP that it always returns false, though the file is there as I can remove the if statement and it shows up fine.

$filename = $_SERVER['DOCUMENT_ROOT']."/images/profilepictures/1.png";
if (file_exists($filename) == true)
{
    $output .= '<img src="'.$filename.'" alt="profile picture" width="200"/>';
}

The $filename echos as:

/home/content/k/e/r/kernelkev/html/images/profilepictures/1.png

I have been googling this and most answers are to use the DOCUMENT_ROOT, but it still does not work for me.

Could anyone shed some light on this as it is really annoying me now.


This seemed to fix it...

$filename = "/images/profilepictures/1.png";
if (file_exists("..".$filename))
{
    $output .= '<img src="'.$filename.'" alt="profile picture" width="150"/>';
}

I have no idea why, but there we go.

file_exists not working if the folder is forbidden. Try change your folder priviledge to 777 or 755 ?

sudo chmod 777 -R images/ 

If you use open_basedir in php.ini and use file_exists for file outside open_basedir path, you will not be warned at log and file_exists returns false even if file really exists. file_exists will have trouble finding your file if the file permissions are not read enabled for 'other' when not owned by your php user. I thought I was having trouble with a directory name having a space in it (/users/andrew/Pictures/iPhoto Library/AlbumData.xml) but the reality was that there weren't read permissions on Pictures, iPhoto Library or AlbumData.xml. Once I fixed that, file_exists worked. These will work fine for us with no problem as we confiugured server properly

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