Codeigniter "file_exists ($ filename)"

Need help with codeigniter, I think file_exists is for the server path, not the url. but my image folder is at the same level as the application and the system folder. didn’t find anything on Google, please help

$url=base_url(); $filename="$url/upload/$id.jpg"; if (file_exists($filename)){ echo "<li><img src=\"".$url."upload/$id.jpg\" width=\"40\" height=\"40\" /></li>"; }else{ echo "<li><img src=\"http://www.mydomain.com/haha/image/noimg.png\" width=\"40\" height=\"40\" /></li>"; } 
+8
file path codeigniter
source share
1 answer

Codeigniter always runs on index.php , so all paths are relative to it. You can use any of the following values ​​if upload/ is at the index.php level:

  • file_exists("upload/$id.jpg")

  • file_exists("./upload/$id.jpg")

  • file_exists(FCPATH."upload/$id.jpg")

FCPATH is the constant that Codeigniter sets that contain the absolute path to your index.php .

As a side note, I prefer is_file() when checking files, since file_exists() returns true in directories. In addition, you can see if getimagesize() returns FALSE to make sure you have an image.

+40
source share

All Articles