简体   繁体   中英

PHP - file_exists doesn't work even if file exists in directory

Im trying to verify if file exists in directory. When I use this code it works - image is displayed:

<?php 

  $imgId=0; 
  $filename='../uploadedimages/project-'.$item->id.'-'.$imgId;

  echo "<img src='".$filename."' ></img>";

?>  

When I use the same code with file_exists function, it doesn't work:

<?php 

  $imgId=0; 
  $filename='../uploadedimages/project-'.$item->id.'-'.$imgId;

  if (file_exists($filename)) {                             
    echo "<img src='".$filename."' ></img>";                
  }

?>  

My question is simple: WTF??

You can use: $_SERVER['DOCUMENT_ROOT'] to know where you are.

And then try something like this:

$filename=$_SERVER['DOCUMENT_ROOT'].'uploadedimages/project-'.$item->id.'-'.$imgId;

But first you need to make sure the path exist.

on linux server, try to change file access permission to 755 before using file_exists function

chmod 755 filename.ext

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