Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 即使文件存在(远程URL),file_exists()也返回false_Php - Fatal编程技术网

Php 即使文件存在(远程URL),file_exists()也返回false

Php 即使文件存在(远程URL),file_exists()也返回false,php,Php,我的文件\u exists()返回false,即使提供了要检查的图像https://www.google.pl/logos/2012/haring-12-hp.png存在。为什么? 下面我将介绍准备在localhost上启动的全部失败PHP代码: $filename = 'https://www.google.pl/logos/2012/haring-12-hp.png'; echo "<img src=" . $filename . " />"; if (file_exists($

我的
文件\u exists()
返回false,即使提供了要检查的图像
https://www.google.pl/logos/2012/haring-12-hp.png
存在。为什么?

下面我将介绍准备在localhost上启动的全部失败PHP代码:

$filename = 'https://www.google.pl/logos/2012/haring-12-hp.png';
echo "<img src=" . $filename . " />";
if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}
$filename='0https://www.google.pl/logos/2012/haring-12-hp.png';
回声“;
如果(文件_存在($filename)){
回显“文件$filename存在”;
}否则{
回显“文件$filename不存在”;
}
从PHP5.0.0开始,这个函数也可以与一些URL包装器一起使用。请参阅支持的协议和包装器,以确定哪些包装器支持stat()系列功能

从开始:


您需要的是类似于
url\u存在的东西
。请参阅
文件\u exists
文档中的注释:

以下是发布的示例之一:

<?php
    function url_exists($url){
        $url = str_replace("http://", "", $url);
        if (strstr($url, "/")) {
            $url = explode("/", $url, 2);
            $url[1] = "/".$url[1];
        } else {
            $url = array($url, "/");
        }

        $fh = fsockopen($url[0], 80);
        if ($fh) {
            fputs($fh,"GET ".$url[1]." HTTP/1.1\nHost:".$url[0]."\n\n");
            if (fread($fh, 22) == "HTTP/1.1 404 Not Found") { return FALSE; }
            else { return TRUE;    }

        } else { return FALSE;}
    }
?>

一个更好的if语句,它不看http版本

$file_headers = @get_headers($remote_filename);    
if (stripos($file_headers[0],"404 Not Found") >0  || (stripos($file_headers[0], "302 Found") > 0 && stripos($file_headers[7],"404 Not Found") > 0)) {
//throw my exception or do something
}

那么,你拥有谷歌,嗯<代码>检查文件或目录是否存在。仅限本地。URL:
http://services.runescape.com/m=itemdb_rs/3716_obj_sprite.gif?id=2
?URL是什么并不重要
http
https
不支持
stat()
。这个例子非常糟糕。最好只使用cURL发出
HEAD
请求。如果链接而不是图像(不存在)转到404自定义错误页,会怎么样?嘿,请查看代码的新更改。现在检查它是否登录到自定义404页面。k我必须将标题从
if($file\u headers[0]='HTTP/1.04notfound'){
更改为
if($file\u headers[0]='HTTP/1.1404 notfound'){
谢谢,你真的帮了我的忙。好吧……我对if条件做了一些调整……if(strop($file\u headers[0],'200')>1或strop($file\u headers[0],'304')>1{echo“文件$filename存在”}可能不是完美的解决方案,但…:)我不得不将if语句更改为
if(strpos($file_headers[0],'404')){//not exists}else{//exist}
code`,因为Szymon-Toda提到了同一点。
$filename= 'https://www.google.pl/logos/2012/haring-12-hp.png';
$file_headers = @get_headers($filename);

if($file_headers[0] == 'HTTP/1.0 404 Not Found'){
      echo "The file $filename does not exist";
} else if ($file_headers[0] == 'HTTP/1.0 302 Found' && $file_headers[7] == 'HTTP/1.0 404 Not Found'){
    echo "The file $filename does not exist, and I got redirected to a custom 404 page..";
} else {
    echo "The file $filename exists";
}
$file_headers = @get_headers($remote_filename);    
if (stripos($file_headers[0],"404 Not Found") >0  || (stripos($file_headers[0], "302 Found") > 0 && stripos($file_headers[7],"404 Not Found") > 0)) {
//throw my exception or do something
}
    $filename = "http://im.rediff.com/money/2011/jan/19sld3.jpg";

    $file_headers = @get_headers($filename);

    if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
    //return false; 
    echo "file not found";
    }else {
    //return true;  
    echo "file found";

    }
function check_file ($file){

    if ( !preg_match('/\/\//', $file) ) {
        if ( file_exists($file) ){
            return true;
        }
    }

    else {

        $ch = curl_init($file);
        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;

    }

    return false;

}