9

Need help with the codeigniter, I think file_exists is for server path, not for url. but my image folder got same level with application and system folder. find nothing 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>"; } 
1
  • 2
    One thing, file_exists is for server paths, not urls. So you wouldn't use base_url(), you'd use APPPATH Commented Feb 9, 2012 at 21:04

1 Answer 1

52

Codeigniter is always running on index.php, so all paths are relative from there. You can use any of the following, assuming upload/ is at the same level as index.php:

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

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

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

FCPATH is a constant that Codeigniter sets which contains the absolute path to your index.php.

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

Sign up to request clarification or add additional context in comments.

4 Comments

my data have the image full path..eg..(localhost/media/newsdata/20150923/Entertainment/Casspture.PNG) it is worng when i use file_exists($thisfile), it was always return false..please
Use only the server path: file_exists('media/newsdata/20150923/Entertainment/Casspture.PNG')
what if the user has encrypted the name of file while uploading it then how should we check? @WesleyMurch
@getimagesize() will return false if there is no file existing and hide a warning message

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.