php 如何从url检查文件是否存在

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

How to check if a file exists from a url

phpfilecurlfsockopenfile-exists

提问by David

I need to check if a particular file exists on a remote server. Using is_file()and file_exists()doesn't work. Any ideas how to do this quickly and easily?

我需要检查远程服务器上是否存在特定文件。使用is_file()file_exists()不起作用。任何想法如何快速轻松地做到这一点?

回答by patrick

You don't need CURL for that... Too much overhead for just wanting to check if a file exists or not...

你不需要 CURL ......只是想检查文件是否存在太多开销......

Use PHP's get_header.

使用PHP 的 get_header

$headers=get_headers($url);

Then check if $result[0] contains 200 OK (which means the file is there)

然后检查 $result[0] 是否包含 200 OK (这意味着文件在那里)

A function to check if a URL works could be this:

检查 URL 是否有效的函数可能是这样的:

function UR_exists($url){
   $headers=get_headers($url);
   return stripos($headers[0],"200 OK")?true:false;
}

/* You can test a URL like this (sample) */
if(UR_exists("http://www.amazingjokes.com/"))
   echo "This page exists";
else
   echo "This page does not exist";

回答by Amila

You have to use CURL

你必须使用卷曲

function does_url_exists($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($code == 200) {
        $status = true;
    } else {
        $status = false;
    }
    curl_close($ch);
    return $status;
}

回答by Daniel Kossmann

I've just found this solution:

我刚刚找到了这个解决方案:

if(@getimagesize($remoteImageURL)){
    //image exists!
}else{
    //image does not exist.
}

Source: http://www.dreamincode.net/forums/topic/11197-checking-if-file-exists-on-remote-server/

来源:http: //www.dreamincode.net/forums/topic/11197-checking-if-file-exists-on-remote-server/

回答by Daniel de coder

Hi according to our test between 2 different servers the results are as follows:

您好,根据我们在 2 个不同服务器之间的测试,结果如下:

using curl for checking 10 .png files (each about 5 mb) was on average 5.7 secs. using header check for the same thing took average of 7.8 seconds!

使用 curl 检查 10 个 .png 文件(每个大约 5 mb)平均需要 5.7 秒。对同一件事使用标题检查平均需要 7.8 秒!

So in our test curl was much faster if you have to check larger files!

因此,在我们的测试中,如果您必须检查较大的文件,curl 会快得多!

our curl function is:

我们的 curl 函数是:

function remote_file_exists($url){
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if( $httpCode == 200 ){return true;}
    return false;
}

here is our header check sample:

这是我们的标题检查示例:

function UR_exists($url){
   $headers=get_headers($url);
   return stripos($headers[0],"200 OK")?true:false;
}

回答by Tom

You can use the function file_get_contents();

您可以使用函数 file_get_contents();

if(file_get_contents('https://example.com/example.txt')) {
    //File exists
}

回答by fivedigit

Do a request with curl and see if it returns a 404 status code. Do the request using the HEAD request method so it only returns the headers without a body.

使用 curl 发出请求,看看它是否返回 404 状态代码。使用 HEAD 请求方法执行请求,因此它只返回没有正文的标头。

回答by Андрей Рабой

    $headers = get_headers((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://" . $_SERVER[HTTP_HOST] . '/uploads/' . $MAIN['id'] . '.pdf');
    $fileExist = (stripos($headers[0], "200 OK") ? true : false);
    if ($fileExist) {
    ?>
    <a class="button" href="/uploads/<?= $MAIN['id'] ?>.pdf" download>скачать</a> 
    <? }
    ?>