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

I am trying to check if a file exists in a directory. When I use this code it works - the 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 ??

+3


source to share


2 answers


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 exists.

+2


source


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



chmod 755 filename.ext

0


source







All Articles