-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTemporaryUploadFile.php
More file actions
81 lines (72 loc) · 2.38 KB
/
TemporaryUploadFile.php
File metadata and controls
81 lines (72 loc) · 2.38 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
* TemporaryUploadFile
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
App::uses('File', 'Utility');
App::uses('TemporaryFolder', 'Files.Utility');
/**
* Class TemporaryUploadFile
*
* 作業用テンポラリフォルダをつくって自動的にその中にアップロードファイルを配置するクラス
* プロセス終了時にテンポラリフォルダごと自動的に削除される。
*
* @see FileUploadComponent FileUploadComponent::getTemporaryUploadFile()で利用される
*
* # 利用例
* $file = new TemporaryUploadFile($_FILE['import_csv']);
* echo $file->path; // ファイルのフルパス
* echo $file->error; // アップロード時のエラー情報
*/
class TemporaryUploadFile extends File {
/**
* @var int アップロード時のエラー情報
*/
public $error;
/**
* @var TemporaryFolder ファイルの配置されるテンポラリフォルダのインスタンス
*/
public $temporaryFolder;
/**
* @var string アップロードされた元ファイル名
*/
public $originalName = null;
/**
* TemporaryUploadFile constructor.
*
* アップロードファイルは、自動的に作成されたテンポラリフォルダに配置される。
* インスタンス破棄時にテンポラリフォルダ毎ファイルも削除される
* ファイル名は自動的にハッシュしたものに書き換わる。
*
* @param array $file アップロードファイルの配列
* @throws InternalErrorException
*/
public function __construct($file) {
$this->temporaryFolder = new TemporaryFolder();
$path = $file['tmp_name'];
$this->originalName = $file['name'];
$destFileName = Security::hash(mt_rand() . microtime(), 'md5') . '.' . pathinfo(
$file['name'],
PATHINFO_EXTENSION
);
$result = $this->_moveFile($path, $this->temporaryFolder->path . DS . $destFileName);
if ($result === false) {
throw new InternalErrorException('move_uploaded_file failed');
}
$this->error = $file['error'];
parent::__construct($this->temporaryFolder->path . DS . $destFileName);
}
/**
* ファイル移動
*
* @param string $path 移動元
* @param string $destPath 移動先
* @return bool
*/
protected function _moveFile($path, $destPath) {
return move_uploaded_file($path, $destPath);
}
}