其他教程

其他教程

Products

当前位置:首页 > 其他教程 >

PHP分片上传代码实战

GG网络技术分享 2025-03-18 16:17 1


php后端处理代码

function import_excel(){

set_time_limit(0);

if(isset($_FILES[\"file\"]) && ($_FILES[\"file\"][\"error\"] == 0)){

//获取文件名

$name = $_POST[\"name\"];

$type = pathinfo($name);

$type = strtolower($type[\"extension\"]);

if($type !== \'xlsx\' && $type !== \'xls\'){

exit(json_encode(array(\'error\'=>1, \'msg\'=>\'不允许上传的文件类型\')));

}

//获取大小

$size = $_POST[\"size\"];

//获取文件类型

$type= $_POST[\"type\"];

//获取文件最后修改时间

$lastModifiedDate= $_POST[\"lastModifiedDate\"];

//获取分片总数

$chunks= $_POST[\"chunks\"];

//获取当前分片索引

$chunk= $_POST[\"chunk\"];

// 文件保存路径

$upload = ROOT_PATH.\'Uploads/xls\';;

RecursiveMkdir( $upload );

// 临时文件保存路径(分片)

$tmp = ROOT_PATH.\'Uploads/xls/tmp\';

RecursiveMkdir( $tmp);

// 判断文件夹是否存在,不存在则创建

//如果不分片的话直接保存

if (!isset($chunks)) {

//将上传的文件保存到指定目录下

move_uploaded_file($_FILES[\"file\"][\"tmp_name\"], $upload.\"/\".$name);

$res=$this->load_excel($upload.\"/\".$name);

if($res){

exit(json_encode(array(\'error\'=>1, \'msg\'=>$res)));

}else{

exit(json_encode(array(\'error\'=>0, \'msg\'=>\'数据导入成功!\')));

}

//输出信息

// echo \"--- 文件上传完毕 ---\\n\";

// echo \"文件名:\".$name.\"\\n\";

// echo \"文件大小:\".$size.\"\\n\";

// echo \"文件类型:\".$type.\"\\n\";

// echo \"文件最后修改时间:\".$lastModifiedDate;

} else {

// 如果分片的话先把分片存储到tmp文件夹下

move_uploaded_file($_FILES[\"file\"][\"tmp_name\"], $tmp.\"/\".$name.\".tmp\".$chunk);

// echo \"--- 分片上传完毕 ---\\n\";

// 判断所有分片是否都上传完毕了

$complete = true;

for($i = 0; $i < $chunks; $i++) {

if(!file_exists($tmp.\"/\".$name.\".tmp\".$i)){

$complete = false;

break;

}

}

//如果所有分片都有的话就开始合并

if ($complete) {

$fp = fopen($upload.\"/\".$name, \"ab\");

for($i = 0; $i < $chunks; $i++) {

$tmp_file = $tmp.\"/\".$name.\".tmp\".$i;

$handle = fopen($tmp_file, \"rb\");

fwrite($fp, fread($handle, filesize($tmp_file)));

fclose($handle);

unset($handle);

unlink($tmp_file);//合并完毕的文件就删除

}

$res=$this->load_excel($upload.\"/\".$name);

if($res){

exit(json_encode(array(\'error\'=>1, \'msg\'=>$res)));

}else{

exit(json_encode(array(\'error\'=>0, \'msg\'=>\'数据导入成功!\')));

}

// echo \"--- 文件合并完毕 ---\\n\";

}

}

}

}

/**

* 递归生成目录

*/

function RecursiveMkdir($path) {

if (!file_exists($path)) {

RecursiveMkdir(dirname($path));

@mkdir($path, 0777);

}

}

前端引入webuploader代码

<link rel=\"stylesheet\" type=\"text/css\" href=\"__PUBLIC__/ueditor/third-party/webuploader/webuploader.css\">

<script type=\"text/javascript\" src=\"__PUBLIC__/ueditor/third-party/webuploader/webuploader.min.js\"></script>

前端触发按钮html代码

<button type=\"button\" name=\"\" data-export=\"3\" value=\"3\" class=\"layui-btn layui-btn-sm\" id=\"orderImportXls\">导入订单</button>

前端样式代码

#orderImportXls input{opacity:0;filter:alpha(opacity=0);}

.webuploader-pick{background: none;padding: 0;}

.webuploader-pick-hover{background: none;padding: 0;}

前端功能代码

//文件信息显示区域

var $list = $(\'#thelist\');

//当前状态

var state = \'pending\';

//初始化Web Uploader

var uploader = WebUploader.create({

// swf文件路径

swf: \'__PUBLIC__/ueditor/third-party/webuploader/Uploader.swf\',

// 文件接收服务端。

server: \"{:U(\'Order/import_excel\')}\",

// 选择文件的按钮。可选。

// 内部根据当前运行是创建,可能是input元素,也可能是flash.

pick: {

id: \'#orderImportXls\',

multiple:false,

innerHTML: \'订单导入\'

},

// 开启分片上传。

chunked: true,

//每个分片的大小(这里设为2M)

chunkSize:2*1024*1024,

accept: {

extensions: \"xls,xlsx\",

mimeTypes: \".xls,.xlsx\"

},duplicate:true //支持重复选择上传

});

// 当有文件被添加进队列的时候(选择文件后调用)

uploader.on( \'fileQueued\', function( file ) {

uploader.upload();

$list.append( \'<div id=\"\' + file.id + \'\" class=\"item\">\' +

\'<p class=\"state\">等待上传...</p>\' +

\'</div>\' );

});

// 文件上传过程中创建进度条实时显示。

uploader.on( \'uploadProgress\', function( file, percentage ) {

var $li = $( \'#\'+file.id );

$li.find(\'p.state\').text(\'上传中(\' + parseInt(percentage * 100) + \'%)\');

});

// 文件上传成功后会调用

uploader.on( \'uploadSuccess\', function( file ,response) {

console.log(\'文件上传成功后会调用\',file);

console.log(response.msg);

if(response.error>0){

layer.msg(response.msg,{icon: 2,time: 3000});

}else{

layer.msg(response.msg,function (){

var location = window.location;

location.href = location.pathname + location.search;});

}

$list.empty();

});

// 文件上传失败后会调用

uploader.on( \'uploadError\', function( file ) {

console.log(file);

$( \'#\'+file.id ).find(\'p.state\').text(\'上传出错\');

});

// 文件上传完毕后会调用(不管成功还是失败)

uploader.on( \'uploadComplete\', function( file ) {

$( \'#\'+file.id ).find(\'.progress\').fadeOut();

});

// all事件(所有的事件触发都会响应到)

uploader.on( \'all\', function( type ) {

if ( type === \'startUpload\' ) {

state = \'uploading\';

} else if ( type === \'stopUpload\' ) {

state = \'paused\';

} else if ( type === \'uploadFinished\' ) {

state = \'done\';

}

// if ( state === \'uploading\' ) {

// $btn.text(\'暂停上传\');

// } else {

// $btn.text(\'开始上传\');

// }

});

支持上传添加头信息

uploader.on(\'uploadBeforeSend\', function (obj, data,headers) {

//添加头参数

headers = $.extend(headers, {

\"HTTP_X_REQUESTED_WITH\": \"xmlhttprequest\",

});

});

点击指定按钮触发上传

$(\'#importGoods\').on(\'click\',function(){

$(\'.webuploader-element-invisible\').trigger(\'click\');

});

标签: 分片 上传

提交需求或反馈

Demand feedback