codeigniter“file_exists($ filename)” [英] Codeigniter "file_exists($filename)"
问题描述
需要帮助codeigniter,我认为file_exists是服务器路径,而不是url。但我的图像文件夹具有相同的水平与应用程序和系统文件夹。在Google上找不到任何内容,请帮助。
$ 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>;
}
Codeigniter总是运行 index.php
,因此所有路径都是相对的。假设 upload /
与 index.php
处于同一级别,您可以使用以下任何一种方法:
-
file_exists(upload / $ id.jpg)
-
file_exists(./ upload / $ id.jpg)
-
file_exists(FCPATH。upload / $ id.jpg)
FCPATH
是一个常量,Codeigniter设置包含 index.php
。
另外,在检查文件时,我更喜欢 is_file()
c $ c> file_exists()在目录上返回true。此外,您可能想要查看 getimagesize()
是否返回 FALSE
,以确保您有图像。 p>
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>";
}
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.
这篇关于codeigniter“file_exists($ filename)”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!