简体   繁体   English

如何使用URL检查服务器上是否存在图像

[英]How to check if Image exists on server using URL

I am trying to check whether image file is exist on server Or not. 我正在尝试检查服务器上是否存在图像文件。 I am getting image path with other server. 我正在与其他服务器建立映像路径。

Please check below code which I've tried, 请检查以下我尝试过的代码,

$urlCheck = getimagesize($resultUF['destination']);

if (!is_array($urlCheck)) {
  $resultUF['destination'] = NULL;
}

But, it shows below warning 但是,它显示以下警告

Warning: getimagesize(http://www.example.com/example.jpg) [function.getimagesize]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in

Is there any way to do so? 有什么办法吗?

Thanks 谢谢

$url = 'http://www.example.com/example.jpg)';
print_r(get_headers($url));

It will give an array. 它将给出一个数组。 Now you can check the response to see if image exists or not 现在您可以检查响应以查看图像是否存在

You need to check that the file is exist regularly on server or not.you should used: 您需要检查文件是否在服务器上定期存在。您应该使用:

is_file .For example is_file 。例如

$url="http://www.example.com/example.jpg"; $ URL = “http://www.example.com/example.jpg”;

if(is_file($url))
{
echo "file exists on server";
}
else
{
echo "file not exists on server ";
}

Fastest & efficient Solution for broken or not found images link 最快和有效的解决方案,以解决损坏或找不到的图像链接
i recommend you that don't use getimagesize() because it will 1st download image then it will check images size+if this will not image then it will throw exception so use below code 我建议您不要使用getimagesize(),因为它将首先下载图像,然后将检查图像大小,如果不显示图像,则会抛出异常,因此请使用以下代码

if(checkRemoteFile($imgurl))
{
//found url, its mean
echo "this is image";
}

function checkRemoteFile($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    // don't download content
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if(curl_exec($ch)!==FALSE)
    {
        return true;
    }
    else
    {
        return false;
    }
}

Note: this current code help you to identify broken or not found url image this will not help you to identify image type or headers 注意:此当前代码可帮助您识别损坏的或未找到的网址图片,这将无法帮助您识别图片类型或标题

问题是该图像可能不存在,或者您没有直接访问该图像的权限 ,否则您必须将无效位置指向该图像。

you can use file_get_contents . 您可以使用file_get_contents This will cause php to issue a warning along side of returning false . 这将导致php在返回false同时发出警告。 You may need to handle such warning display to ensure the user interface doesn't get mixed up with it. 您可能需要处理此类警告显示,以确保用户界面不会与之混淆。

 if (file_get_contents($url) === false) {
       //image not foud
   }

Use fopen function 使用fopen功能

if (@fopen($resultUF['destination'], "r")) {
    echo "File Exist";
} else {
    echo "File Not exist";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM