php 下载到本地

180it 2021-02-28 PM 112℃ 0条
    /**下载到本地
     */
    static function httpcopy($url, $file = "", $timeout = 600)
    {
        if (empty($file))
            return false;
        $dir = pathinfo($file, PATHINFO_DIRNAME);
        !is_dir($dir) && @mkdir($dir, 0755, true);
        $url = str_replace(" ", "%20", $url);

        if (function_exists('curl_init'))
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_REFERER, $url);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            
            if (FALSE !== strpos($url, 'https'))
            {
//                echo 'https : '.$url;ob_flush();  flush();
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            }

            $temp = curl_exec($ch);
//            $result = curl_getinfo($ch,CURLINFO_HTTP_CODE); //返回值,错误调试用
            curl_close($ch);
            if (@file_put_contents($file, $temp) && !curl_error($ch))
            {
                return $file;
            } else
            {
                $opts = array(
                    "http" => array(
                        "method" => "GET",
                        "header" => "",
                        "timeout" => $timeout)
                );

                $context = stream_context_create($opts);
                if (@copy($url, $file, $context))
                {
                    //$http_response_header
                    return $file;
                } else
                {
                    return false;
                }
                return false;
            }
        } else
        {
            $opts = array(
                "http" => array(
                    "method" => "GET",
                    "header" => "",
                    "timeout" => $timeout)
            );
            $context = stream_context_create($opts);
            if (@copy($url, $file, $context))
            {
                //$http_response_header
                return $file;
            } else
            {
                return false;
            }
        }
    }

    
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

php 下载到本地