3

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??

Tomas Turan
  • 1,195
  • 3
  • 17
  • 27

2 Answers2

2

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.

JuanSedano
  • 1,025
  • 8
  • 14
0

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

chmod 755 filename.ext