php 检查文件是否存在于php中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4253487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:24:54  来源:igfitidea点击:

check if file exists in php

phpfile-exists

提问by anonymous

if (!(file_exists(http://mysite.com/images/thumbnail_1286954822.jpg))) {   
$filefound = '0';                         
}

why won't this work?

为什么这行不通?

回答by Haim Evgi

if (!file_exists('http://example.com/images/thumbnail_1286954822.jpg')) {   
$filefound = '0';
}

回答by Ignacio Vazquez-Abrams

  1. The function expects a string.

  2. file_exists()does not work properly with HTTP URLs.

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

  2. file_exists()HTTP URL 不能正常工作。

回答by IRSHAD

file_existschecks whether a file exist in the specified path or not.

file_exists检查指定路径中是​​否存在文件。

Syntax:

句法:

file_exists ( string $filename )

Returns TRUEif the file or directory specified by filename exists; FALSEotherwise.

返回TRUE文件名指定的文件或目录是否存在;FALSE除此以外。

$filename = BASE_DIR."images/a/test.jpg";
if (file_exists($filename)){
    echo "File exist.";
}else{
    echo "File does not exist.";
}

Another alternative method you can use getimagesize(), it will return 0(zero) if file/directory is not available in the specified path.

您可以使用 getimagesize() 的另一种替代方法,如果指定路径中的文件/目录不可用,它将返回 0(零)。

if (@getimagesize($filename)) {...}

回答by JJJ

Based on your comment to Haim, is this a file on your own server? If so, you need to use the file system path, not url (e.g. file_exists( '/path/to/images/thumbnail.jpg' )).

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

回答by Nashir

for me also the file_exists() function is not working properly. So I got this alternative solution. Hope this one help someone

对我来说,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";
    }

回答by Pradeep Kumar Prabaharan

You can also use PHPget_headers()function.

您还可以使用PHPget_headers()函数。

Example:

例子:

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";