[英]check if file exists in php
if (!(file_exists(http://mysite.com/images/thumbnail_1286954822.jpg))) {
$filefound = '0';
}
为什么这行不通?
if (!file_exists('http://example.com/images/thumbnail_1286954822.jpg')) {
$filefound = '0';
}
该函数需要一个字符串。
file_exists()
不能与 HTTP URL 一起正常工作。
file_exists检查指定路径中是否存在文件。
句法:
file_exists ( string $filename )
如果文件名指定的文件或目录存在,则返回TRUE
; 否则为FALSE
。
$filename = BASE_DIR."images/a/test.jpg";
if (file_exists($filename)){
echo "File exist.";
}else{
echo "File does not exist.";
}
您可以使用 getimagesize() 的另一种替代方法,如果指定路径中的文件/目录不可用,它将返回 0(零)。
if (@getimagesize($filename)) {...}
根据您对 Haim 的评论,这是您自己服务器上的文件吗? 如果是这样,您需要使用文件系统路径,而不是 url(例如file_exists( '/path/to/images/thumbnail.jpg' )
)。
您也可以使用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";
对我来说,file_exists() 函数也不能正常工作。 所以我得到了这个替代解决方案。 希望这对某人有所帮助
$path = 'http://localhost/admin/public/upload/video_thumbnail/thumbnail_1564385519_0.png';
if (@GetImageSize($path)) {
echo 'File exits';
} else {
echo "File doesn't exits";
}
检查下面的代码
if ($user->image) {
$filename = "images/" . $user->image;
if (file_exists($filename)) {
echo '<br />';
echo "File exist.";
} else {
echo '<br />';
echo "File does not exist.";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.