file_get_contents() how to fix error “Failed to open stream”, “No such file”

后端 未结 8 936
傲寒
傲寒 2020-12-01 17:35

I\'m getting the following error when I try to run my PHP script:

failed to open stream: No such file or directory in C:\\wamp\\www\\LOF\\Data.php on

8条回答
  •  天涯浪人
    2020-12-01 18:20

    The actual problem of this error has nothing to do with file_get_content, the problem is the requested url if the url is not throwing content of the page and redirecting the request to some where else file_get_content says "Failed to open stream", just before file_get_contents check whether the url is working and not redirecting, here is the code:

    function checkRedirect404($url) {

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
        curl_setopt($ch, CURLOPT_URL, $url);
        $out = curl_exec($ch);
    
        // line endings is the wonkiest piece of this whole thing
        $out = str_replace("\r", "", $out);
    
        // only look at the headers
        $headers_end = strpos($out, "\n\n");
        if( $headers_end !== false ) { 
            $out = substr($out, 0, $headers_end);
        }   
    
        $headers = explode("\n", $out);
        foreach($headers as $header) {
            if( substr($header, 0, 10) == "Location: " ) { 
                $target = substr($header, 10);
    
                //echo "Redirects: $target
    "; return true; } } return false;

    }

提交回复
热议问题