検索
ホームページphp教程php手册PHP ダウンロード リモート ファイル クラス (ブレークポイント再開ダウンロードをサポート)

PHP ダウンロード リモート ファイル クラス (ブレークポイント再開ダウンロードをサポート)

Jun 13, 2016 pm 12:26 PM
newobjectphpダウンロードコード説明書コピーサポート書類ブレークポイント親切リモートで

使い方は簡単:

コードをコピー コードは次のとおりです。


$object = new httpdownload(); $object- >set_byfile($file)%N#H#%;//パスを含むサーバー ファイル名
$object->filename = $filename;//ダウンロード ファイル名は
$object として保存されます-> ;down​​load();


3. ソース ファイル:

コードをコピーします コードは次のとおりです。



class httpdownload {
var $data = null;
var $data_len = 0;
var $data_mod = 0;
var $data_type = 0;
var $data_section = 0; //セクションのダウンロード
var $sentSize=0;
var $handler = array('auth' => null);
var $use_resume = true;
var $use_autoexit = false;
var $use_auth = false;
var $filename = null;
var $mime = null;
var $bufsize = 2048;
var $seek_start = 0;
var $seek_end = -1;
var $totalsizeref = 0;
var $bandwidth = 0;
var $speed = 0;
関数の初期化() {
グローバル $HTTP_SERVER_VARS;
if ($this->use_auth) // 認証を使用します
{
if (!$this->_auth()) // 認証なし
{
header('WWW -Authenticate: Basic realm="ユーザー名とパスワードを入力してください"');
header('HTTP/1.0 401 Unauthorized');
header('ステータス: 401 Unauthorized');
if ($this->use_autoexit) exit();
false を返します。
}
}
if ($this->mime == null) $this->mime = "application/octet-stream"; //デフォルト mime
if (isset($_SERVER_VARS['HTTP_RANGE']) || isset($HTTP_SERVER_VARS['HTTP_RANGE']))
{
if (isset($HTTP_SERVER_VARS['HTTP_RANGE']) )) $seek_range = substr($HTTP_SERVER_VARS['HTTP_RANGE'] , strlen('bytes='));
else $seek_range = substr($_SERVER['HTTP_RANGE'] , strlen('bytes='));
$range =explode('-',$seek_range);
if ($range[0] > 0)
{
$this->seek_start = intval($range[0]);
}
if ($range[1] > 0) $this->seek_end = intval($range[1]);
else $this->seek_end = -1;
if (!$this->use_resume)
{
$this->seek_start = 0;
//header("HTTP/1.0 404 Bad Request");
//header("ステータス: 400 不正なリクエスト");
//終了;
// false を返します。
}
else
{
$this->data_section = 1;
}
}
else
{
$this->seek_start = 0;
$this->seek_end = -1;
}
$this->sentSize=0;
true を返します。
}
function header($size,$seek_start=null,$seek_end=null) {
header('Content-type: ' . $this->mime);
header('Content-Disposition:attachment; filename="' . $this->filename . '"');
header('Last-Modified: ' . date('D, d M Y H:i:s GMT' , $this->data_mod));
if ($this->data_section && $this->use_resume)
{
header("HTTP/1.0 206 部分コンテンツ");
header("ステータス: 206 部分コンテンツ");
header('Accept-Ranges: バイト');
header("Content-Range: bytes $seek_start-$seek_end/$size");
header("Content-Length: " . ($seek_end - $seek_start 1));
}
else
{
header("Content-Length: $size");
}
}
function download_ex($size)
{
if (!$this->initialize()) return false;
ignore_user_abort(true);
//ここでシーク終了を使用します
if ($this->seek_start > ($size - 1)) $this->seek_start = 0;
if ($this->seek_end seek_end = $size - 1;
$this->header($size,$seek,$this->seek_end);
$this->data_mod = time();
true を返します。
}
function download() {
if (!$this->initialize()) return false;
試してください
{
error_log("begin downloadn", 3,"/usr/local/www/apache22/LOGS/apache22_php.err");
$seek = $this->seek_start;
$speed = $this->speed;
$bufsize = $this->bufsize;
$packet = 1;
//クリーンアップを行います
@ob_end_clean();
$old_status =ignore_user_abort(true);
@set_time_limit(0);
$this->帯域幅 = 0;
$size = $this->data_len;
if ($this->data_type == 0) //ファイルからダウンロード
{
$size = filesize($this->data);
if ($seek > ($size - 1)) $seek = 0;
if ($this->filename == null) $this->filename = Basename($this->data);
$res = fopen($this->data,'rb');
if ($seek) fseek($res , $seek);
if ($this->seek_end seek_end = $size - 1;
$this->header($size,$seek,$this->seek_end); //常に最後のシークを使用します
$size = $this->seek_end - $seek 1;
while (!(connection_aborted() || connection_status() == 1) && $size > 0)
{
if ($size {
echo fread($res , $size);
$this->帯域幅 = $size;
$this->sentSize =$size;
}
else
{
echo fread($res , $bufsize);
$this->帯域幅 = $bufsize;
$this->sentSize =$bufsize;
}
$size -= $bufsize;
フラッシュ();
if ($speed > 0 && ($this->bandwidth > $speed*$packet*1024))
{
sleep(1);
$packet ;
}
}
fclose($res);
}
elseif ($this->data_type == 1) //文字列からダウンロード
{
if ($seek > ($size - 1)) $seek = 0;
if ($this->seek_end seek_end = $this->data_len - 1;
$this->data = substr($this->data , $seek , $this->seek_end - $seek 1);
if ($this->filename == null) $this->filename = time();
$size = strlen($this->data);
$this->header($this->data_len,$seek,$this->seek_end);
while (!connection_aborted() && $size > 0) {
if ($size {
$this->bandwidth = $size;
$this->sentSize =$size;
}
else
{
$this->bandwidth = $bufsize;
$this->sentSize =$bufsize;
}
echo substr($this->data , 0 , $bufsize);
$this->data = substr($this->data , $bufsize);
$size -= $bufsize;
フラッシュ();
if ($speed > 0 && ($this->bandwidth > $speed*$packet*1024))
{
sleep(1);
$packet ;
}
}
} else if ($this->data_type == 2) {
//リダイレクトヘッダーを送信するだけです
header('location: ' . $this-> ;データ);
}
if($this->totalsizeref==$this->sentSize )error_log("ダウンロード終了n", 3,"/usr/local/www/apache22/LOGS/apache22_php.err") ;
else error_log("ダウンロードはキャンセルされましたn", 3,"/usr/local/www/apache22/LOGS/apache22_php.err");
if ($this->use_autoexit) exit();
//古いステータスを復元
ignore_user_abort($old_status);
set_time_limit(ini_get("max_execution_time"));
}
catch(Exception $e)
{
error_log("ダウンロードをキャンセルn".$e, 3,"/usr/local/www/apache22/LOGS/apache22_php.err");
}
true を返します。
}
function set_byfile($dir) {
if (is_readable($dir) && is_file($dir)) {
$this->data_len = 0;
$this->data = $dir;
$this->data_type = 0;
$this->data_mod = filemtime($dir);
$this->totalsizeref = filesize($dir);
true を返します。
} それ以外の場合は false を返します。
}
function set_bydata($data) {
if ($data == '') return false;
$this->data = $data;
$this->data_len = strlen($data);
$this->data_type = 1;
$this->data_mod = time();
true を返します。
}
function set_byurl($data) {
$this->data = $data;
$this->data_len = 0;
$this->data_type = 2;
true を返します。
}
function set_lastmodtime($time) {
$time = intval($time);
if ($time $this->data_mod = $time;
}
function _auth() {
if (!isset($_SERVER['PHP_AUTH_USER'])) return false;
if (isset($this->handler['auth']) && function_exists($this->handler['auth']))
{
return $this->handler[ 'auth']('auth' , $_SERVER['PHP_AUTH_USER'],$_SERVER['PHP_AUTH_PW']);
}
else は true を返します。 //ハンドラーを使用する必要があります
}
}
?>


声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SecLists

SecLists

SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

mPDF

mPDF

mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。