Http-https php File download

Source: Internet
Author: User

http

function Httpdownload ($url, $path = ", $filename =", $timeout = all, $type = 0) {if ($url = = ") {            return false;                }//Get remote file data if ($type = = = 0) {$ch = Curl_init ();                curl_setopt ($ch, Curlopt_url, $url);                curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_connecttimeout, $timeout);//Maximum Execution time curl_setopt ($ch, Curlopt_timeout, $timeout);//most                Long wait time $file = curl_exec ($ch);            Curl_close ($ch);                } if ($type = = = 1) {Ob_start ();                ReadFile ($url);                $file = Ob_get_contents ();            Ob_end_clean ();            } if ($type = = = 2) {$file = file_get_contents ($url); }//Determine if the downloaded data is empty download timeout problem if (empty ($file)) {throw new \exception ("Download error, unable to get download file!            "); }//no path specifiedDefault current path if ($path = = =) {$path = "./";            }//If the name is empty if ($filename = = = "") {$filename = MD5 ($file);            }//Get the suffix name $ext = substr ($url, Strrpos ($url, '. '));            if ($ext && strlen ($ext) < 5) {$filename. = $ext; }//Prevent "/" is not added $path = RTrim ($path, "/").            "/";            Var_dump ($path. $filename);d ie ();            $FP 2 = @fopen ($path. $filename, ' a ');            Fwrite ($fp 2, $file);            Fclose ($fp 2);            echo "Finish";    return $filename; }

https:

 function Httpsdownload ($url, $path = ", $filename =", $timeout = all, $type = 0) {if ($url = = ") {        return false;            }//Get remote file data if ($type = = = 0) {$ch = Curl_init ();            curl_setopt ($ch, Curlopt_url, $url);            curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_connecttimeout, $timeout);//Maximum Execution time curl_setopt ($ch, Curlopt_timeout, $timeout);//Longest wait            Inter-curl_setopt ($ch, Curlopt_ssl_verifypeer, false);            curl_setopt ($ch, Curlopt_ssl_verifyhost, false);            $file = curl_exec ($ch);        Curl_close ($ch);            } if ($type = = = 1) {Ob_start ();            ReadFile ($url);            $file = Ob_get_contents ();        Ob_end_clean ();        } if ($type = = = 2) {$file = file_get_contents ($url); }//Determine if the downloaded data is empty download timeout problem if (empty ($file)) {throw new \exception ("Download error, unable to get download file!        "); }       If no path is specified, the default current path if ($path = = = ") {$path ="./";        }//If the name is empty if ($filename = = = "") {$filename = MD5 ($file);        }//Get the suffix name $ext = substr ($url, Strrpos ($url, '. '));        if ($ext && strlen ($ext) < 5) {$filename. = $ext; }//Prevent "/" is not added $path = RTrim ($path, "/").        "/";        Var_dump ($path. $filename);d ie ();        $FP 2 = @fopen ($path. $filename, ' a ');        Fwrite ($fp 2, $file);        Fclose ($fp 2);        echo "Finish";    return $filename; }

  

Http-https php File download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.