PHP:如何检查图像文件是否存在?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7991425/
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-26 03:43:52  来源:igfitidea点击:

PHP: How to check if image file exists?

phpimagefilefile-io

提问by PaperChase

I need to see if a specific image exists on my cdn.

我需要查看我的 CDN 上是否存在特定图像。

I've tried the following and it doesn't work:

我已经尝试了以下方法,但它不起作用:

if (file_exists(http://www.example.com/images/$filename)) {
    echo "The file exists";
} else {
    echo "The file does not exist";
}

Even if the image exists or doesn't exist, it always says "The file exists". I'm not sure why its not working...

即使图像存在或不存在,它也总是说“文件存在”。我不确定为什么它不起作用......

回答by knittl

You need the filename in quotation marks at least (as string):

您至少需要引号中的文件名(作为字符串):

if (file_exists('http://www.mydomain.com/images/'.$filename)) {
 … }

Also, make sure $filenameis properly validated. And then, it will only work when allow_url_fopenis activated in your PHP config

此外,请确保$filename已正确验证。然后,它仅allow_url_fopen在您的 PHP 配置中激活时才有效

回答by Jeffrey Jenkinson

if (file_exists('http://www.mydomain.com/images/'.$filename)) {}

This didn't work for me. The way I did it was using getimagesize.

这对我不起作用。我这样做的方式是使用 getimagesize。

$src = 'http://www.mydomain.com/images/'.$filename;

if (@getimagesize($src)) {

Note that the '@' will mean that if the image does not exist (in which case the function would usually throw an error: getimagesize(http://www.mydomain.com/images/filename.png) [function.getimagesize]: failed) it will return false.

请注意,'@' 表示如果图像不存在(在这种情况下,该函数通常会抛出错误:)getimagesize(http://www.mydomain.com/images/filename.png) [function.getimagesize]: failed它将返回 false。

回答by Rizerzero

Well, file_existsdoes not say if a file exists, it says if a path exists. ???????

好吧,file_exists不是说文件是否存在,而是说路径是否存在。???????

So, to check if it is a file then you should use is_filetogether with file_existsto know if there is really a file behind the path, otherwise file_existswill return truefor any existing path.

所以,要检查它是否是一个文件,那么你应该is_file一起使用with file_exists来知道路径后面是否真的有一个文件,否则file_exists将返回true任何现有路径。

Here is the function i use :

这是我使用的功能:

function fileExists($filePath)
{
      return is_file($filePath) && file_exists($filePath);
}

回答by pinaldesai

Try like this:

像这样尝试:

$file = '/path/to/foo.txt'; // 'images/'.$file (physical path)

if (file_exists($file)) {
    echo "The file $file exists";
} else {
    echo "The file $file does not exist";
}

回答by jfindley

Here is the simplest way to check if a file exist:

这是检查文件是否存在的最简单方法:

if(is_file($filename)){
    return true; //the file exist
}else{
    return false; //the file does not exist
}

回答by Your Common Sense

A thing you have to understand first: you have no files.
A file is a subject of a filesystem, but you are making your request using HTTP protocol which supports no files but URLs.

您必须首先了解一件事:您没有文件
文件是文件系统的主题,但您使用 HTTP 协议发出请求,该协议不支持文件,只支持 URL。

So, you have to request an unexisting file using your browser and see the response code. if it's not 404, you are unable to use any wrappers to see if a file exists and you have to request your cdn using some other protocol, FTP for example

因此,您必须使用浏览器请求一个不存在的文件并查看响应代码。如果不是 404,则您无法使用任何包装器来查看文件是否存在,并且您必须使用其他协议(例如 FTP)请求您的 CDN

回答by realmag777

public static function is_file_url_exists($url) {
        if (@file_get_contents($url, 0, NULL, 0, 1)) {
            return 1;
        }

        return 0;           
    }

回答by master_gracey

If the file is on your local domain, you don't need to put the full URL. Only the path to the file. If the file is in a different directory, then you need to preface the path with "."

如果文件在您的本地域中,则不需要输入完整的 URL。只有文件的路径。如果文件位于不同的目录中,则需要在路径前加上“.”。

$file = './images/image.jpg';
if (file_exists($file)) {}

Often times the "." is left off which will cause the file to be shown as not existing, when it in fact does.

很多时候是“。” 被忽略,这将导致文件显示为不存在,而实际上确实存在。

回答by Wilt

There is a major difference between is_fileand file_exists.

is_file和之间有很大的区别file_exists

is_filereturns true for (regular) files:

is_file对于(常规)文件返回 true:

Returns TRUEif the filename exists and is a regular file, FALSEotherwise.

如果文件名存在并且是常规文件,则返回TRUE,否则返回FALSE

file_existsreturns true for both files and directories:

file_exists对文件和目录都返回 true:

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

如果文件名指定的文件或目录存在,则返回TRUE;否则为



Note:Check also this stackoverflow questionfor more information on this topic.

注意:查看此 stackoverflow 问题以获取有关此主题的更多信息。

回答by Dmitriy Kravchuk

You have to use absolute path to see if the file exists.

您必须使用绝对路径来查看文件是否存在。

$abs_path = '/var/www/example.com/public_html/images/';
$file_url = 'http://www.example.com/images/' . $filename;

if (file_exists($abs_path . $filename)) {

    echo "The file exists. URL:" . $file_url;

} else {

    echo "The file does not exist";

}

If you are writing for CMS or PHP framework then as far as I know all of them have defined constant for document root path.

如果您正在为 CMS 或 PHP 框架编写代码,那么据我所知,它们都为文档根路径定义了常量。

e.g WordPress uses ABSPATH which can be used globally for working with files on the server using your code as well as site url.

例如,WordPress 使用 ABSPATH,它可以在全球范围内使用您的代码以及站点 url 来处理服务器上的文件。

Wordpress example:

WordPress 示例:

$image_path = ABSPATH . '/images/' . $filename;
$file_url = get_site_url() . '/images/' . $filename;

if (file_exists($image_path)) {

    echo "The file exists. URL:" . $file_url;

} else {

    echo "The file does not exist";

}

I'm going an extra mile here :). Because this code would no need much maintenance and pretty solid, I would write it with as shorthand if statement:

我要在这里加倍努力:)。因为这段代码不需要太多维护并且非常可靠,所以我将它写成简写的 if 语句:

$image_path = ABSPATH . '/images/' . $filename;
$file_url = get_site_url() . '/images/' . $filename;

echo (file_exists($image_path))?'The file exists. URL:' . $file_url:'The file does not exist';

Shorthand IF statement explained:

速记 IF 语句解释:

$stringVariable = ($trueOrFalseComaprison > 0)?'String if true':'String if false';