當前位置:首頁 » 編程語言 » 斷點下載PHP

斷點下載PHP

發布時間: 2022-04-08 05:27:13

php 怎麼寫斷點續傳 有插件也行 求大神

  1. HTTP協議里, 對斷點下載有支持.
    斷點上傳單純靠PHP 是做不了的.
    因為普通的瀏覽器端沒那功能.(上傳的時候 還是會整個文件編碼發送)
    想實現的話 , 客戶端需要插件了,
    客戶端可以使用flex實現. 服務端, PHP可以寫個webservice 接受文件.

  2. 可以在網上找找. 關鍵字: "flex php 上傳"
    大文件上傳, 走 http 是比較費勁兒的. 不穩定是關鍵.

② php 斷點續傳

<?php

/**

* PHP-HTTP斷點續傳實現

* @param string $path: 文件所在路徑

* @param string $file: 文件名

* @return void

*/

function download($path,$file) {

$real = $path.'/'.$file;

if(!file_exists($real)) {

return false;

}

$size = filesize($real);

$size2 = $size-1;

$range = 0;

if(isset($_SERVER['HTTP_RANGE'])) {

header('HTTP /1.1 206 Partial Content');

$range = str_replace('=','-',$_SERVER['HTTP_RANGE']);

$range = explode('-',$range);

$range = trim($range[1]);

header('Content-Length:'.$size);

header('Content-Range: bytes '.$range.'-'.$size2.'/'.$size);

} else {

header('Content-Length:'.$size);

header('Content-Range: bytes 0-'.$size2.'/'.$size);

}

header('Accenpt-Ranges: bytes');

header('application/octet-stream');

header("Cache-control: public");

header("Pragma: public");

//解決在IE中下載時中文亂碼問題

$ua = $_SERVER['HTTP_USER_AGENT'];

if(preg_match('/MSIE/',$ua)) {

$ie_filename = str_replace('+','%20',urlencode($file));

header('Content-Dispositon:attachment; filename='.$ie_filename);

} else {

header('Content-Dispositon:attachment; filename='.$file);

}

$fp = fopen($real,'rb+');

fseek($fp,$range);

while(!feof($fp)) {
set_time_limit(0);

print(fread($fp,1024));

flush();

ob_flush();

}

fclose($fp);

}

/*End of PHP*/

③ 剛開始接觸php沒多久, php實現了一個支持斷點續傳的文件下載,當用迅雷來下載的時候會出現頁面無法顯示

用系統自帶的瀏覽器下載方可
請參考

補充:
不想直接下載的話,你要找到一個可以復制會話的下載工具。目前我還沒見過哪個下載工具可以做到。
因為在你把下載鏈接拖到迅雷的時候,迅雷建立了一個新的HTTP通訊到目標,也就等於建立了一個新的會話。而要完成這樣的下載,需要的是一個合法的會話(會員登錄的那個會話),迅雷新建的那個會話顯然是一個普通訪客(未登錄)的會話,所以伺服器肯定會拒絕下載請求。
這就是原因。如有不明白,可以發HI問我

④ 斷點續傳幾個小問題(PHP)

第一個問題應該是迅雷的判斷,這個我覺得沒辦法
第二個,你確定那個輸出為真的話,echo出的東西在頁面上的地方可能沒找對,你可以試一下用JS的彈窗:
echo '<script>alert("Yes");</script>";

⑤ php斷點調試的幾種方法

具體代碼如下:
<?php
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, '');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>
PHP 獨特的語法混合了C、Java、Perl以及PHP自創的語法。
它可以比CGI或者Perl更快速地執行動態網頁。用PHP做出的動態頁面與其他的編程語言相比,PHP是將程序嵌入到HTML(標准通用標記語言下的一個應用)文檔中去執行,
執行效率比完全生成HTML標記的CGI要高許多;
PHP還可以執行編譯後代碼,編譯可以達到加密和優化代碼運行,使代碼運行更快。

⑥ php斷點續傳代碼怎麼寫

下載伺服器支持斷續傳才能
用php也就是用HTTP協議去下載數據,斷點續傳也就是請求前取得文件大小,再下,下到那裡記下來,本地初始化一個同樣大小的文件,下次請求文件時,給出這個地址,就能從下載伺服器得到對應的文件,再寫入你上次的文件的對應位置

⑦ 如何在本地端實現php對大文件的斷點續下載

function download($path,$file) {

$real = $path.'/'.$file;

if(!file_exists($real)) {

return false;

}

$size = filesize($real);

$size2 = $size-1;

$range = 0;

if(isset($_SERVER['HTTP_RANGE'])) {

header('HTTP /1.1 206 Partial Content');

$range = str_replace('=','-',$_SERVER['HTTP_RANGE']);

$range = explode('-',$range);

$range = trim($range[1]);

header('Content-Length:'.$size);

header('Content-Range: bytes '.$range.'-'.$size2.'/'.$size);

} else {

header('Content-Length:'.$size);

header('Content-Range: bytes 0-'.$size2.'/'.$size);

}

header('Accenpt-Ranges: bytes');

header('application/octet-stream');

header("Cache-control: public");

header("Pragma: public");

⑧ 誰知道關於PHP的斷點續傳第三方插件哪裡有下載謝謝

php有這東西嗎?php屬於http協議,http是不支持斷點續傳的,大文件上傳可以用SWF上傳,有個插件SWFUpload

⑨ php 斷點續傳怎麼做啊!

功能:支持斷點續傳的下載,能計算傳輸率,能控制傳輸率

簡易使用方法:

$object = new httpdownload();
$object->set_byfile($file)%N#H#%;//伺服器文件名,包括路徑
$object->filename = $filename;//下載另存為的文件名
$object->download();

3.源文件:

<?
class httpdownload {
var $data = null;
var $data_len = 0;
var $data_mod = 0;
var $data_type = 0;
var $data_section = 0; //section download
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;
function initialize() {
global $HTTP_SERVER_VARS;
if ($this->use_auth) //use authentication {
if (!$this->_auth()) //no authentication {
header('WWW-Authenticate: Basic realm="Please enter your username and password"');
header('HTTP/1.0 401 Unauthorized');
header('status: 401 Unauthorized');
if ($this->use_autoexit) exit();
return false;
}
}
if ($this->mime == null) $this->mime = "application/octet-stream"; //default mime
if (isset($_SERVER['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("Status: 400 Bad Request");
//exit;
//return false;
} else {
$this->data_section = 1;
}
} else {
$this->seek_start = 0;
$this->seek_end = -1;
}
$this->sentSize=0;
return 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 /G/M/T' , $this->data_mod));
if ($this->data_section && $this->use_resume) {
header("HTTP/1.0 206 Partial Content");
header("Status: 206 Partial Content");
header('Accept-Ranges: bytes');
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);
//Use seek end here
if ($this->seek_start > ($size - 1)) $this->seek_start = 0;
if ($this->seek_end <= 0) $this->seek_end = $size - 1;
$this->header($size,$seek,$this->seek_end);
$this->data_mod = time();
return true;
}
function download() {
if (!$this->initialize()) return false;
try {
error_log("begin download/n", 3,"/usr/local/www/apache22/LOGS/apache22_php.err");
$seek = $this->seek_start;
$speed = $this->speed;
$bufsize = $this->bufsize;
$packet = 1;
//do some clean up
@ob_end_clean();
$old_status = ignore_user_abort(true);
@set_time_limit(0);
$this->bandwidth = 0;
$size = $this->data_len;
if ($this->data_type == 0) //download from a file {
$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) $this->seek_end = $size - 1;
$this->header($size,$seek,$this->seek_end); //always use the last seek
$size = $this->seek_end - $seek + 1;
while (!(connection_aborted() || connection_status() == 1) && $size > 0) {
if ($size < $bufsize) {
echo fread($res , $size);
$this->bandwidth += $size;
$this->sentSize+=$size;
} else {
echo fread($res , $bufsize);
$this->bandwidth += $bufsize;
$this->sentSize+=$bufsize;
}
$size -= $bufsize;
flush();
if ($speed > 0 && ($this->bandwidth > $speed*$packet*1024)) {
sleep(1);
$packet++;
}
}
fclose($res);
}
elseif ($this->data_type == 1) //download from a string
{
if ($seek > ($size - 1)) $seek = 0;
if ($this->seek_end < $seek) $this->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 < $bufsize) {
$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;
flush();
if ($speed > 0 && ($this->bandwidth > $speed*$packet*1024)) {
sleep(1);
$packet++;
}
}
} else if ($this->data_type == 2) {
//just send a redirect header
header('location: ' . $this->data);
}
if($this->totalsizeref==$this->sentSize )error_log("end download/n", 3,"/usr/local/www/apache22/LOGS/apache22_php.err");
else error_log("download is canceled/n", 3,"/usr/local/www/apache22/LOGS/apache22_php.err");
if ($this->use_autoexit) exit();
//restore old status
ignore_user_abort($old_status);
set_time_limit(ini_get("max_execution_time"));
}
catch(Exception $e) {
error_log("cancel download/n".$e, 3,"/usr/local/www/apache22/LOGS/apache22_php.err");
}
return 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);
return true;
} else return 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();
return true;
}
function set_byurl($data) {
$this->data = $data;
$this->data_len = 0;
$this->data_type = 2;
return true;
}
function set_lastmodtime($time) {
$time = intval($time);
if ($time <= 0) $time = 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 return true; //you must use a handler
}
}
?>

熱點內容
電腦都連不上伺服器說ip不對 發布:2025-01-19 12:52:24 瀏覽:625
linux解壓到文件夾命令 發布:2025-01-19 12:43:20 瀏覽:425
父母訪問 發布:2025-01-19 12:33:05 瀏覽:794
加密文件如何編輯 發布:2025-01-19 12:31:18 瀏覽:219
androiddpi 發布:2025-01-19 12:21:15 瀏覽:655
伺服器鎖了怎麼解 發布:2025-01-19 12:06:58 瀏覽:301
DH演算法使用 發布:2025-01-19 11:57:30 瀏覽:932
Wcl上傳如何選擇伺服器 發布:2025-01-19 11:17:24 瀏覽:763
如何編程簡單給伺服器發一個指令 發布:2025-01-19 11:16:44 瀏覽:806
python控制台亂碼 發布:2025-01-19 10:55:38 瀏覽:364