本文实例讲述了php大文件分块上传功能。分享给大家供大家参考,具体如下:
前端代码
使用file.slice将文件进行分割,然后分别进行异步上传。
<!doctype html><html lang="zh-cn"> <head> <meta chart="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title><%= title %></title> <!-- zui --> <link rel="stylesheet" href="/d/file/titlepic/zui.min.css" rel="external nofollow" > </head> <body> <div class="container" style="margin-top: 30px;"> <form class="form-inli误会 飞轮海ne" method="post" enctype="multipart/form-data"> <div class="form-group"> <input type="file" id="filebig" class="form-control"> </div> <button type="submit" class="btn btn-primary">提交</button> </form> </div> <!-- zui javascript 依赖 jquery --> <script src="http://zui.xy/asts/jquery.js"></script> <!-- zui 标准版压缩后的 javascript 文件 --> <script src="http://zui.xy/dist/js/zui.min.js"></script> <script type="text/javascript"> $('form').submit(function() { let file = $(":file")[0].files[0]; let filename = file.name; let filesize = file.size; console.log('filesize',filesize); let blocksize = 0.9*1024*1024; let num = math.ceil(filesize/blocksize); let start = 0; let end = 0; for(let i=1;i<=num;i++){ end = blocksize*i; if(end > filesize){ end = filesize; } let block = file.slice(start,end); start = end; 便宜的英文 let fd = ne英雄联盟圣枪游侠w formdata(); fd.append('block',block); fd.append('name',filename); fd.append('total',num); fd.append('index',i); $.ajax({ url:"upload.php", type:"post", data:fd, async:true, processdata:fal, contenttype:fal, success:(res)爱马=>{ console.log('res_'+i+":"); console.log(res); } }) } return fal; }); </script> </body></html>
后项目投标书端代码
在所有的文件上传成功之后,合并生成原来的大文件
<?php$name = $_post['name'];$index = $_post['index'];$total = $_post['total'];echo "name:".$name.php_eol;echo "index:".$index.php_eol;echo "total:".$total.php_eol;move_uploaded_file($_files['block']['tmp_name'],'upload/'.$name."_".$index);$list = scandir('upload');$num = count($list)-2;echo "cur_num:".$num.php_eol;if($num == $total){ echo "upload done".php_eol; echo $cmd = "cat upload/'{$name}_'* > upload/'{$name}'"; shell_exec($cmd);}
本文发布于:2023-04-07 14:27:42,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/08b62529987a83f6fe30fc7fdd7aadc1.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:PHP大文件分块上传功能实例详解.doc
本文 PDF 下载地址:PHP大文件分块上传功能实例详解.pdf
留言与评论(共有 0 条评论) |