博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP,javascript实现大文件上传
阅读量:4553 次
发布时间:2019-06-08

本文共 1855 字,大约阅读时间需要 6 分钟。

HTML代码

 
Document

  PHP代码

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();

 

转载于:https://www.cnblogs.com/isuben/p/7061431.html

你可能感兴趣的文章
TCP/IP协议学习笔记
查看>>
(iphone开发)从网络读取图片并显示
查看>>
js屏蔽浏览器(IE和FireFox)的刷新和右键等功能
查看>>
OC:copy 与 retain 的区别
查看>>
线程的新建、中断与终止
查看>>
jQuery1.7.1 API的整理和总结
查看>>
C#对DataTable里数据筛选排序的方法
查看>>
Confluence 6 设置公共访问
查看>>
堆内存管理
查看>>
PIE保护绕过
查看>>
牛客假日团队赛11 A 级数求和
查看>>
2019百度之星初赛一 1005 Seq HDU - 6672 (打表找规律)
查看>>
[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher D - Cyclic Nacklace HDU - 3746(循环节kmp)...
查看>>
Por Costel and the Match Gym - 100923H(经典种类并查集)
查看>>
Happy 2006 POJ - 2773(欧几里得算法 互素问题)
查看>>
[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher E - Period HDU - 1358(循环节kmp)
查看>>
[kuangbin带你飞]专题十二 基础DP1 F - Piggy-Bank HDU - 1114(完全背包)
查看>>
Trailing Zeroes (I) LightOJ - 1028(唯一分解 因子个数)
查看>>
洛谷题 P3366 【模板】最小生成树
查看>>
Farey Sequence POJ - 2478 (欧拉函数 前缀和)
查看>>