if (!(file_exists(http://mysite.com/images/thumbnail_1286954822.jpg))) {
$filefound = '0';
}
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?
Hai*_*vgi 56
if (!file_exists('http://mysite.com/images/thumbnail_1286954822.jpg')) {
$filefound = '0';
}
Run Code Online (Sandbox Code Playgroud)
IRS*_*HAD 12
file_exists检查指定路径中是否存在文件.
句法:
file_exists ( string $filename )
Run Code Online (Sandbox Code Playgroud)
TRUE如果filename指定的文件或目录存在,则返回; FALSE除此以外.
$filename = BASE_DIR."images/a/test.jpg";
if (file_exists($filename)){
echo "File exist.";
}else{
echo "File does not exist.";
}
Run Code Online (Sandbox Code Playgroud)
另一种可以使用getimagesize()的替代方法,如果文件/目录在指定路径中不可用,它将返回0(零).
if (@getimagesize($filename)) {...}
Run Code Online (Sandbox Code Playgroud)
您还可以使用PHP get_headers()函数。
例子:
function check_file_exists_here($url){
$result=get_headers($url);
return stripos($result[0],"200 OK")?true:false; //check if $result[0] has 200 OK
}
if(check_file_exists_here("http://www.mywebsite.com/file.pdf"))
echo "This file exists";
else
echo "This file does not exist";
Run Code Online (Sandbox Code Playgroud)
根据您对Haim的评论,这是您自己服务器上的文件吗?如果是这样,您需要使用文件系统路径,而不是url(例如file_exists( '/path/to/images/thumbnail.jpg' )).
| 归档时间: |
|
| 查看次数: |
107797 次 |
| 最近记录: |