-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileBreakUpload.php
67 lines (56 loc) · 1.62 KB
/
FileBreakUpload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
* 分片上传工具类
*/
class FileBreakUpload
{
private $filePath = './upload';//上传目录
private $tmpPath = './tmp';//上传临时目录
private $blobNum; //第几块文件
private $totalBlobNum;//文件块总数
private $fileName; //文件名
public function __construct($filePath, $tmpPath, $blobNum, $totalBlobNum, $fileName)
{
$this->filePath = $filePath;
$this->tmpPath = $tmpPath;
$this->blobNum = $blobNum;
$this->totalBlobNum;
$this->fileName = $fileName;
$this->uploadBlobFile();
$this->mergeUploadFile();
}
/**
* 创建目录
*/
private function createUplaodDirByPath()
{
if (!file_exists($this->filePath)){
mkdir($this->filePath, 777);
}
}
/**
* 上传分片文件
*/
private function uploadBlobFile()
{
$this->createUplaodDirByPath();
$fileBolbName = $this->filePath . '/'. $this->fileName. '__'. $this->blobNum;
move_uploaded_file($this->tmpPath, $fileBolbName);
}
private function mergeUploadFile()
{
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);
}
}
private function deleteBlobFile()
{
for ($i=1; $i<=$this->totalBlobNum; $i++){
@unlink($this->filePath . '/'. $this->fileName. '__'.$i);
}
}
}