suchen
Heimphp教程PHP源码PHP unterstützt das Herunterladen von Breakpoint-Lebensläufen und das Herunterladen von Chunks

<?php
 
/**
 * 支持断点下载的类
 */
class downloader {
 
    /**
     * download file to local path
     *
     * @param       $url
     * @param       $save_file
     * @param int   $speed
     * @param array $headers
     * @param int   $timeout
     * @return bool
     * @throws Exception
     */
    static function get($url, $save_file, $speed = 10240, $headers = array(), $timeout = 10) {
        $url_info = self::parse_url($url);
        if (!$url_info[&#39;host&#39;]) {
            throw new Exception(&#39;Url is Invalid&#39;);
        }
 
        // default header
        $def_headers = array(
            &#39;Accept&#39;          => &#39;*/*&#39;,
            &#39;User-Agent&#39;      => &#39;Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)&#39;,
            &#39;Accept-Encoding&#39; => &#39;gzip, deflate&#39;,
            &#39;Host&#39;            => $url_info[&#39;host&#39;],
            &#39;Connection&#39;      => &#39;Close&#39;,
            &#39;Accept-Language&#39; => &#39;zh-cn&#39;,
        );
 
        // merge heade
        $headers = array_merge($def_headers, $headers);
        // get content length
        $content_length = self::get_content_size($url_info[&#39;host&#39;], $url_info[&#39;port&#39;], $url_info[&#39;request&#39;], $headers, $timeout);
 
        // content length not exist
        if (!$content_length) {
            throw new Exception(&#39;Content-Length is Not Exists&#39;);
        }
        // get exists length
        $exists_length = is_file($save_file) ? filesize($save_file) : 0;
        // get tmp data file
        $data_file = $save_file . &#39;.data&#39;;
        // get tmp data
        $exists_data = is_file($data_file) ? json_decode(file_get_contents($data_file), 1) : array();
        // check file is valid
        if ($exists_length == $content_length) {
            $exists_data && @unlink($data_file);
            return true;
        }
        // check file is expire
        if ($exists_data[&#39;length&#39;] != $content_length || $exists_length > $content_length) {
            $exists_data = array(
                &#39;length&#39; => $content_length,
            );
        }
        // write exists data
        file_put_contents($data_file, json_encode($exists_data));
 
        try {
            $download_status = self::download_content($url_info[&#39;host&#39;], $url_info[&#39;port&#39;], $url_info[&#39;request&#39;], $save_file, $content_length, $exists_length, $speed, $headers, $timeout);
            if ($download_status) {
                @unlink($data_file);
            }
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
        return true;
    }
 
    /**
     * parse url
     *
     * @param $url
     * @return bool|mixed
     */
    static function parse_url($url) {
        $url_info = parse_url($url);
        if (!$url_info[&#39;host&#39;]) {
            return false;
        }
        $url_info[&#39;port&#39;]    = $url_info[&#39;port&#39;] ? $url_info[&#39;host&#39;] : 80;
        $url_info[&#39;request&#39;] = $url_info[&#39;path&#39;] . ($url_info[&#39;query&#39;] ? &#39;?&#39; . $url_info[&#39;query&#39;] : &#39;&#39;);
        return $url_info;
    }
 
    /**
     * download content by chunk
     *
     * @param $host
     * @param $port
     * @param $url_path
     * @param $headers
     * @param $timeout
     */
    static function download_content($host, $port, $url_path, $save_file, $content_length, $range_start, $speed, &$headers, $timeout) {
        $request = self::build_header(&#39;GET&#39;, $url_path, $headers, $range_start);
        $fsocket = @fsockopen($host, $port, $errno, $errstr, $timeout);
        stream_set_blocking($fsocket, TRUE);
        stream_set_timeout($fsocket, $timeout);
        fwrite($fsocket, $request);
        $status = stream_get_meta_data($fsocket);
        if ($status[&#39;timed_out&#39;]) {
            throw new Exception(&#39;Socket Connect Timeout&#39;);
        }
        $is_header_end = 0;
        $total_size    = $range_start;
        $file_fp       = fopen($save_file, &#39;a+&#39;);
        while (!feof($fsocket)) {
            if (!$is_header_end) {
                $line = @fgets($fsocket);
                if (in_array($line, array("\n", "\r\n"))) {
                    $is_header_end = 1;
                }
                continue;
            }
            $resp        = fread($fsocket, $speed);
            $read_length = strlen($resp);
            if ($resp === false || $content_length < $total_size + $read_length) {
                fclose($fsocket);
                fclose($file_fp);
                throw new Exception(&#39;Socket I/O Error Or File Was Changed&#39;);
            }
            $total_size += $read_length;
            fputs($file_fp, $resp);
            // check file end
            if ($content_length == $total_size) {
                break;
            }
            sleep(1);
            // for test
            //break;
        }
        fclose($fsocket);
        fclose($file_fp);
        return true;
 
    }
 
    /**
     * get content length
     *
     * @param $host
     * @param $port
     * @param $url_path
     * @param $headers
     * @param $timeout
     * @return int
     */
    static function get_content_size($host, $port, $url_path, &$headers, $timeout) {
        $request = self::build_header(&#39;HEAD&#39;, $url_path, $headers);
        $fsocket = @fsockopen($host, $port, $errno, $errstr, $timeout);
        stream_set_blocking($fsocket, TRUE);
        stream_set_timeout($fsocket, $timeout);
        fwrite($fsocket, $request);
        $status = stream_get_meta_data($fsocket);
        $length = 0;
        if ($status[&#39;timed_out&#39;]) {
            return 0;
        }
        while (!feof($fsocket)) {
            $line = @fgets($fsocket);
            if (in_array($line, array("\n", "\r\n"))) {
                break;
            }
            $line = strtolower($line);
            // get location
            if (substr($line, 0, 9) == &#39;location:&#39;) {
                $location = trim(substr($line, 9));
                $url_info = self::parse_url($location);
                if (!$url_info[&#39;host&#39;]) {
                    return 0;
                }
                fclose($fsocket);
                return self::get_content_size($url_info[&#39;host&#39;], $url_info[&#39;port&#39;], $url_info[&#39;request&#39;], $headers, $timeout);
            }
            // get content length
            if (strpos($line, &#39;content-length:&#39;) !== false) {
                list(, $length) = explode(&#39;content-length:&#39;, $line);
                $length = (int)trim($length);
            }
        }
        fclose($fsocket);
        return $length;
 
    }
 
    /**
     * build header for socket
     *
     * @param     $action
     * @param     $url_path
     * @param     $headers
     * @param int $range_start
     * @return string
     */
    static function build_header($action, $url_path, &$headers, $range_start = -1) {
        $out = $action . " {$url_path} HTTP/1.0\r\n";
        foreach ($headers as $hkey => $hval) {
            $out .= $hkey . &#39;: &#39; . $hval . "\r\n";
        }
        if ($range_start > -1) {
            $out .= "Accept-Ranges: bytes\r\n";
            $out .= "Range: bytes={$range_start}-\r\n";
        }
        $out .= "\r\n";
 
        return $out;
    }
}
 
 
#use age
/*
try {
    if (downloader::get(&#39;http://dzs.aqtxt.com/files/11/23636/201604230358308081.rar&#39;, &#39;test.rar&#39;)) {
        //todo
        echo &#39;Download Succ&#39;;
    }
} catch (Exception $e) {
    echo &#39;Download Failed&#39;;
}
*/
?>

Stellungnahme
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
4 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
4 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
4 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat -Befehle und wie man sie benutzt
4 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

mPDF

mPDF

mPDF ist eine PHP-Bibliothek, die PDF-Dateien aus UTF-8-codiertem HTML generieren kann. Der ursprüngliche Autor, Ian Back, hat mPDF geschrieben, um PDF-Dateien „on the fly“ von seiner Website auszugeben und verschiedene Sprachen zu verarbeiten. Es ist langsamer und erzeugt bei der Verwendung von Unicode-Schriftarten größere Dateien als Originalskripte wie HTML2FPDF, unterstützt aber CSS-Stile usw. und verfügt über viele Verbesserungen. Unterstützt fast alle Sprachen, einschließlich RTL (Arabisch und Hebräisch) und CJK (Chinesisch, Japanisch und Koreanisch). Unterstützt verschachtelte Elemente auf Blockebene (wie P, DIV),

SAP NetWeaver Server-Adapter für Eclipse

SAP NetWeaver Server-Adapter für Eclipse

Integrieren Sie Eclipse mit dem SAP NetWeaver-Anwendungsserver.

WebStorm-Mac-Version

WebStorm-Mac-Version

Nützliche JavaScript-Entwicklungstools

MinGW – Minimalistisches GNU für Windows

MinGW – Minimalistisches GNU für Windows

Dieses Projekt wird derzeit auf osdn.net/projects/mingw migriert. Sie können uns dort weiterhin folgen. MinGW: Eine native Windows-Portierung der GNU Compiler Collection (GCC), frei verteilbare Importbibliotheken und Header-Dateien zum Erstellen nativer Windows-Anwendungen, einschließlich Erweiterungen der MSVC-Laufzeit zur Unterstützung der C99-Funktionalität. Die gesamte MinGW-Software kann auf 64-Bit-Windows-Plattformen ausgeführt werden.

VSCode Windows 64-Bit-Download

VSCode Windows 64-Bit-Download

Ein kostenloser und leistungsstarker IDE-Editor von Microsoft