加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

PHP大文件分割上传 PHP分片上传

发布时间:2021-03-12 22:46:00 所属栏目:编程 来源:网络整理
导读:处事端为什么不能直接传大文件?跟php.ini内里的几个设置有关 虽然不能简朴粗暴的把上面几个值调大,不然处事器内存资源吃光是早晚的题目。 办理思绪 亏得HTML5开放了新的FILE API,也可以直接操纵二进制工具,我们可以直接在赏识器端实现文件切割,凭证早年

处事端为什么不能直接传大文件?跟php.ini内里的几个设置有关

虽然不能简朴粗暴的把上面几个值调大,不然处事器内存资源吃光是早晚的题目。

办理思绪

亏得HTML5开放了新的FILE API,也可以直接操纵二进制工具,我们可以直接在赏识器端实现文件切割,凭证早年的做法就得用Flash的方案,实现起来会贫困许多。

JS思绪 1.监听上传按钮的onchange变乱 2.获取文件的FILE工具 3.把文件的FILE工具举办切割,而且附加到FORMDATA工具中 4.把FORMDATA工具通过AJAX发送随处事器 5.一再3、4步调,直到文件发送完。

PHP思绪 1.成立上传文件夹 2.把文件从上传姑且目次移动到上传文件夹 3.全部的文件块上传完成后,举办文件合成 4.删除文件夹 5.返回上传后的文件路径

DEMO代码

前端部门代码

<div class="jb51code">
<pre class="brush:xhtml;">
<!doctype html>
<html lang="en">

Document

PHP部门代码

public function __construct($tmpPath,$blobNum,$totalBlobNum,$fileName){
$this->tmpPath = $tmpPath;
$this->blobNum = $blobNum;
$this->totalBlobNum = $totalBlobNum;
$this->fileName = $fileName;

$this->moveFile();
$this->fileMerge();

}

//判定是否是最后一块,假如是则举办文件合成而且删除文件块
private function fileMerge(){
if($this->blobNum == $this->totalBlobNum){
$blob = '';
for($i=1; $i<= $this->totalBlobNum; $i++){
$blob .= file_get_contents($this->filepath.'/'. $this->fileName.'__'.$i);
}
file_put_contents($this->filepath.'/'. $this->fileName,$blob);
$this->deleteFileBlob();
}
}

//删除文件块
private function deleteFileBlob(){
for($i=1; $i<= $this->totalBlobNum; $i++){
@unlink($this->filepath.'/'. $this->fileName.'__'.$i);
}
}

//移动文件
private function moveFile(){
$this->touchDir();
$filename = $this->filepath.'/'. $this->fileName.'__'.$this->blobNum;
move_uploaded_file($this->tmpPath,$filename);
}

//API返回数据
public function apiReturn(){
if($this->blobNum == $this->totalBlobNum){
if(file_exists($this->filepath.'/'. $this->fileName)){
$data['code'] = 2;
$data['msg'] = 'success';
$data['file_path'] = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['DOCUMENT_URI']).str_replace('.','',$this->filepath).'/'. $this->fileName;
}
}else{
if(file_exists($this->filepath.'/'. $this->fileName.'__'.$this->blobNum)){
$data['code'] = 1;
$data['msg'] = 'waiting for all';
$data['file_path'] = '';
}
}
header('Content-type: application/json');
echo json_encode($data);
}

//成立上传文件夹
private function touchDir(){
if(!file_exists($this->filepath)){
return mkdir($this->filepath);
}
}
}

//实例化并获取体系变量传参
$upload = new Upload($_FILES['file']['tmp_name'],$_POST['blob_num'],$_POST['total_blob_num'],$_POST['file_name']);
//挪用要领,返回功效
$upload->apiReturn();

以上就是本文的所有内容,但愿对各人的进修有所辅佐,也但愿各人多多支持编程之家。

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读