HTTP协议里, 对断点下载有支持.
断点上传单纯靠PHP 是做不了的.
因为普通的浏览器端没那功能.(上传的时候 还是会整个文件编码发送)
想实现的话 , 客户端需要插件了,
客户端可以使用flex实现. 服务端, PHP可以写个webservice 接受文件.
可以在网上找找. 关键字: "flex php 上传"
大文件上传, 走 http 是比较费劲儿的. 不稳定是关键.
是实现断点续传,就需要把大文件分割成多个小文件,然后单个上传。传完后在合并。│ merge.PHP –合并文件脚本
│ merge.zip –合并后文件
│ socket.zip –需要分割的文件
│ split.php –分割文件脚本
│
└─split –分割后小文件目录
下面是源码
split.php
<?php
$fp = fopen("socket.zip", "rb")
$filesize = 10
$i = 0
$no = 1
while(!feof($fp))
{
$file = fread($fp, $filesize)
$fp2 = fopen("./split/socket.port".sprintf("%04d",$no).".".$i."-".($i+$filesize).".tmp", "wb")fwrite($fp2, $file, $filesize)
fclose($fp2)
$i+=$filesize+1
$no++
}
fclose($fp)
merge.php
<?php
$filelist = glob('./split/*socket*.tmp')$filesize = 10
//print_r($filelist)
$mergeFileName = 'merg.zip'
unlink($mergeFileName)
$fp2 = fopen($mergeFileName,"w+")
foreach($filelist as $k =>$v)
{
$fp = fopen($v, "rb")
$content = fread($fp, $filesize)
fwrite($fp2, $content, $filesize)
unset($content)
fclose($fp)
echo $k,"\n"
}
fclose($fp2)
<?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*/
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)