-
-
Notifications
You must be signed in to change notification settings - Fork 167
/
FileMerger.php
63 lines (54 loc) · 1.31 KB
/
FileMerger.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
<?php
namespace Pion\Laravel\ChunkUpload;
use Pion\Laravel\ChunkUpload\Exceptions\ChunkSaveException;
class FileMerger
{
/**
* @var bool|resource
*/
protected $destinationFile;
/**
* FileMerger constructor.
*
* @param string $targetFile
*
* @throws ChunkSaveException
*/
public function __construct($targetFile)
{
// open the target file
if (!$this->destinationFile = @fopen($targetFile, 'ab')) {
throw new ChunkSaveException('Failed to open output stream.', 102);
}
}
/**
* Appends given file.
*
* @param string $sourceFilePath
*
* @return $this
*
* @throws ChunkSaveException
*/
public function appendFile($sourceFilePath)
{
// open the new uploaded chunk
if (!$in = @fopen($sourceFilePath, 'rb')) {
@fclose($this->destinationFile);
throw new ChunkSaveException('Failed to open input stream', 101);
}
// read and write in buffs
while ($buff = fread($in, 4096)) {
fwrite($this->destinationFile, $buff);
}
@fclose($in);
return $this;
}
/**
* Closes the connection to the file.
*/
public function close()
{
@fclose($this->destinationFile);
}
}