1 <?php
2 /**
3 * Uplaod.php
4 *
5 * @author Elvyrra SAS
6 * @license http://rem.mit-license.org/ MIT
7 */
8
9 namespace Hawk;
10
11 /**
12 * This class permits to treat AJAX uploads
13 *
14 * @package Core
15 */
16 class Upload{
17 /**
18 * The uploaded files
19 */
20 private $files = array();
21
22 /**
23 * Get an upload instance by the name of the uplaod
24 *
25 * @param string $name the name of the upload
26 *
27 * @return Upload The upload instance
28 */
29 public static function getInstance($name){
30 try{
31 return new self($name);
32 }
33 catch(UploadException $e){
34 return null;
35 }
36 }
37
38 /**
39 * Constructor
40 *
41 * @param string $name the name of the upload
42 */
43 private function __construct($name){
44 $files = App::request()->getFiles();
45 if(empty($files[$name])) {
46 throw new UploadException();
47 }
48
49 if(is_array($files[$name]['name'])) {
50 foreach($files[$name]['name'] as $i => $data){
51 if(!is_file($files[$name]['tmp_name'][$i])) {
52 throw new UploadException();
53 }
54
55 $this->files[$i] = (object) array(
56 'basename' => $files[$name]['name'][$i],
57 'tmpFile' => $files[$name]['tmp_name'][$i],
58 'mime' => $files[$name]['type'][$i],
59 'size' => $files[$name]['size'][$i],
60 'extension' => pathinfo($files[$name]['name'][$i], PATHINFO_EXTENSION)
61 );
62 }
63 }
64 else{
65 if(!is_file($files[$name]['tmp_name'])) {
66 throw new UploadException();
67 }
68
69 $this->files[] = (object) array(
70 'basename' => $files[$name]['name'],
71 'tmpFile' => $files[$name]['tmp_name'],
72 'mime' => $files[$name]['type'],
73 'size' => $files[$name]['size'],
74 'extension' => pathinfo($files[$name]['name'], PATHINFO_EXTENSION)
75 );
76 }
77 }
78
79 /**
80 * Get the uploaded files
81 *
82 * @return array The uploaded files, where each element is a StdClass instance containing the properties : basename, tmpFile, mime, size and extension
83 */
84 public function getFiles(){
85 return $this->files;
86 }
87
88 /**
89 * Get one of the uploaded files.
90 *
91 * @param int $index The index of the uploaded files to get. If not set, this function will return the first (or the only one) uploaded file
92 *
93 * @return StdClass The uploaded file at the given index
94 */
95 public function getFile($index = 0){
96 return $this->files[$index];
97 }
98
99 /**
100 * Move a uploaded file to a directory
101 *
102 * @param StdClass $file The file to move
103 * @param string $directory The directory where to move the file
104 * @param string $basename The basename to apply for the moved file
105 *
106 * @return boolean True if the operation has been computed successfully, and False if an error occured
107 */
108 public function move($file, $directory, $basename = null){
109 if($basename === null) {
110 $basename = $file->basename;
111 }
112
113 return move_uploaded_file($file->tmpFile, $directory . '/' . $basename);
114 }
115 }
116
117 /**
118 * This class describes the exceptions throwed by Upload class
119 *
120 * @package Exceptions
121 */
122 class UploadException extends \Exception{
123 }
124