Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
643 views
in Technique[技术] by (71.8m points)

file - Codeigniter "file_exists($filename)"

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>";
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...