PHP   发布时间:2019-11-10  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP大文件分割上传 PHP分片上传大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

服务端为什么不能直接传大文件?跟php.ini里面的几个配置有关

php;"> upload_max_filesize = 2M //php最大能接受的文件大小 post_max_size = 8M //php能收到的最大POST值' memory_limit = 128M //内存上限 max_execution_time = 30 //最大执行时间

当然不能简单粗暴的把上面几个值调大,否则服务器内存资源吃光是迟早的问题。

解决思路

好在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">

http-equiv="X-UA-Compatible" content="ie=edge"> Document
php">

php部分代码

php;"> php class Upload{ private $filepath = './upload'; //上传目录 private $tmpPath; //php文件临时目录 private $blobNum; //第几个文件块 private $@R_75_10586@lBlobNum; //文件块总数 private $filename; //文件名

public function __construct($tmpPath,$blobNum,$@R_75_10586@lBlobNum,$fileName){
$this->tmpPath = $tmpPath;
$this->blobNum = $blobNum;
$this->@R_75_10586@lBlobNum = $@R_75_10586@lBlobNum;
$this->filename = $filename;

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

}

//判断是否是最后一块,如果是则进行文件合成并且删除文件块
private function fileMerge(){
if($this->blobNum == $this->@R_75_10586@lBlobNum){
$blob = '';
for($i=1; $i<= $this->@R_75_10586@lBlobNum; $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->@R_75_10586@lBlobNum; $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->@R_75_10586@lBlobNum){
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['@R_75_10586@l_blob_num'],$_POST['file_name']);
//调用方法,返回结果
$upload->apiReturn();

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持菜鸟教程。

大佬总结

以上是大佬教程为你收集整理的PHP大文件分割上传 PHP分片上传全部内容,希望文章能够帮你解决PHP大文件分割上传 PHP分片上传所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。