检查php中是否存在文件

ano*_*ous 35 php file-exists

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)

  • @anonymous实际上它不是这样抱歉,你没有要求$ base_url + images/thumbnail_1286954822.jpg你要求http:// mysite com/images/thumbnail_1286954822.jpg (2认同)

Ign*_*ams 30

  1. 该函数需要一个字符串.

  2. file_exists() 使用HTTP URL无法正常工作.


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)


Pra*_*ran 8

您还可以使用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)


JJJ*_*JJJ 5

根据您对Haim的评论,这是您自己服务器上的文件吗?如果是这样,您需要使用文件系统路径,而不是url(例如file_exists( '/path/to/images/thumbnail.jpg' )).