/src/Resource/File.php

https://github.com/andyburton/Sonic-Framework · PHP · 526 lines · 188 code · 191 blank · 147 comment · 19 complexity · ac8d1f080d51eedd14af6fb62ff048e3 MD5 · raw file

  1. <?php
  2. // Define namespace
  3. namespace Sonic\Resource;
  4. // Start File Class
  5. class File
  6. {
  7. /**
  8. * The uploaded file data
  9. * @var array
  10. */
  11. protected $upload = array ();
  12. /**
  13. * The absolute file directory path
  14. * @var string
  15. */
  16. protected $path = FALSE;
  17. /**
  18. * The file name including extension
  19. * @var string
  20. */
  21. protected $filename = FALSE;
  22. /**
  23. * The file name
  24. * @var string
  25. */
  26. protected $name = FALSE;
  27. /**
  28. * The file extension
  29. * @var string
  30. */
  31. protected $extension = FALSE;
  32. /**
  33. * Constructor method
  34. * @param array $file Upload file array
  35. * @return void
  36. */
  37. public function __construct ($file = array ())
  38. {
  39. // Set upload file if there is one
  40. if ($file)
  41. {
  42. $this->Upload ($file);
  43. }
  44. }
  45. /**
  46. * Set the file upload and check for any errors
  47. * @param array $file Uploaded file data
  48. * @return boolean
  49. */
  50. public function Upload ($file)
  51. {
  52. // Set uploaded file
  53. $this->upload = $file;
  54. // If there is an upload error
  55. if ($this->upload['error'] != UPLOAD_ERR_OK)
  56. {
  57. // Set error
  58. new \Sonic\Message ('error', self::_uploadError ($this->upload['error']));
  59. // Return FALSE
  60. return FALSE;
  61. }
  62. // Set file details
  63. $this->filename = $this->upload['name'];
  64. $this->extension = self::_getExtension ($this->filename);
  65. $this->name = self::_getFilename ($this->filename);
  66. // Return TRUE
  67. return TRUE;
  68. }
  69. /**
  70. * Save the uploaded file to a new location
  71. * @param string $path Directory to save file into
  72. * @param string $name New filename
  73. * @param string $extension New extension
  74. * @return boolean
  75. */
  76. public function Save ($path, $name = FALSE, $extension = FALSE)
  77. {
  78. // Set path/name variables
  79. if (!$this->setPath ($path))
  80. {
  81. return FALSE;
  82. }
  83. if ($name)
  84. {
  85. $this->name = $name;
  86. }
  87. if ($extension)
  88. {
  89. $this->extension = $extension;
  90. }
  91. $this->filename = $this->name . '.' . $this->extension;
  92. // Get tmp path
  93. $tmpPath = $this->tmpPath ();
  94. if (!$tmpPath)
  95. {
  96. return FALSE;
  97. }
  98. // Move the file
  99. if (!move_uploaded_file ($tmpPath, $this->path . $this->filename))
  100. {
  101. // Set error
  102. new \Sonic\Message ('error', 'Cannot save uploaded file!');
  103. // Return FALSE
  104. return FALSE;
  105. }
  106. // Return TRUE
  107. return TRUE;
  108. }
  109. /**
  110. * Return the filename including extension
  111. * @return string
  112. */
  113. public function getFilename ()
  114. {
  115. return $this->filename;
  116. }
  117. /**
  118. * Return the filename excluding the extension
  119. * @return string
  120. */
  121. public function getName ()
  122. {
  123. return $this->name;
  124. }
  125. /**
  126. * Return the file absolute directory path
  127. * @return string
  128. */
  129. public function getPath ()
  130. {
  131. return $this->path;
  132. }
  133. /**
  134. * Return the filename extension
  135. * @return string
  136. */
  137. public function getExtension ()
  138. {
  139. return $this->extension;
  140. }
  141. /**
  142. * Return MD5 hash for the file
  143. * @return string
  144. */
  145. public function getMD5 ()
  146. {
  147. return self::_getMD5 ($this->getPath () . $this->getFilename ());
  148. }
  149. /**
  150. * Return an MD5 hash for a file
  151. * @param string $file File path
  152. * @return string
  153. */
  154. public static function _getMD5 ($file)
  155. {
  156. return md5_file ($file);
  157. }
  158. /**
  159. * Set absolute directory path
  160. * @param string $path Directory path
  161. * @return boolean
  162. */
  163. public function setPath ($path)
  164. {
  165. if (!is_dir ($path) && !mkdir ($path, 0755, TRUE))
  166. {
  167. return FALSE;
  168. }
  169. if (!is_writable ($path))
  170. {
  171. return FALSE;
  172. }
  173. $this->path = $path;
  174. return TRUE;
  175. }
  176. /**
  177. * Return the uploaded file tmp path
  178. * @return string|boolean
  179. */
  180. public function tmpPath ()
  181. {
  182. // If there is no uploaded file
  183. if (!$this->upload)
  184. {
  185. // Set error
  186. new \Sonic\Message ('error', 'No file has been uploaded!');
  187. // Return FALSE
  188. return FALSE;
  189. }
  190. // Return the file tmp path
  191. return $this->upload['tmp_name'];
  192. }
  193. /**
  194. * Delete the file
  195. * @return boolean
  196. */
  197. public function Delete ()
  198. {
  199. // If there is a file and path
  200. if ($this->path && $this->filename)
  201. {
  202. // Delete file
  203. return self::_Delete ($this->path . $this->filename);
  204. }
  205. // Return TRUE
  206. return TRUE;
  207. }
  208. /**
  209. * Delete a file
  210. * @param string $file File to delete
  211. * @return boolean
  212. */
  213. public static function _Delete ($file)
  214. {
  215. // If file doesnt exists
  216. if (!self::_Exists ($file))
  217. {
  218. return FALSE;
  219. }
  220. // Delete file
  221. return @unlink ($file);
  222. }
  223. /**
  224. * Return an upload error string
  225. * @param integer $error Error code
  226. * @return string
  227. */
  228. public static function _uploadError ($error)
  229. {
  230. // Return an error dependent on the error number
  231. switch ($error)
  232. {
  233. case UPLOAD_ERR_INI_SIZE:
  234. return 'The filesize cannot be more than ' . ini_get ('post_max_size');
  235. break;
  236. case UPLOAD_ERR_FORM_SIZE:
  237. return 'The filesize cannot be more than ' . $_POST['MAX_FILE_SIZE'];
  238. break;
  239. case UPLOAD_ERR_PARTIAL:
  240. return 'The file was only partially uploaded. Please try again.';
  241. break;
  242. case UPLOAD_ERR_NO_FILE:
  243. return 'No file was selected to upload!';
  244. break;
  245. case UPLOAD_ERR_NO_TMP_DIR:
  246. return 'There is no temporary folder to upload the file to. Please inform the site administrator.';
  247. break;
  248. case UPLOAD_ERR_CANT_WRITE:
  249. return 'Failed to write the file to disk.';
  250. break;
  251. default:
  252. return 'Unknown error.';
  253. break;
  254. }
  255. }
  256. /**
  257. * Return the file extension
  258. * @param string $file Filename to get extension from
  259. * @return string
  260. */
  261. public static function _getExtension ($file)
  262. {
  263. // Get Extension
  264. $base = basename ($file);
  265. $arr = explode ('.', $base);
  266. $ext = strtolower (array_pop ($arr));
  267. // Return the extension
  268. return $ext;
  269. }
  270. /**
  271. * Return the filename without the extension
  272. * @param string $file Filename to get filename from
  273. * @return string
  274. */
  275. public static function _getFilename ($file)
  276. {
  277. // Remove extension
  278. $base = basename ($file);
  279. $arr = explode ('.', $base);
  280. array_pop ($arr);
  281. // Reset the filename
  282. $file = NULL;
  283. // Construct the filename without the extension
  284. foreach ($arr as $part)
  285. {
  286. $file .= $part;
  287. }
  288. // Return the filename
  289. return $file;
  290. }
  291. /**
  292. * Return the file mime type
  293. * @param string $file File path
  294. * @return string|boolean
  295. */
  296. public static function _getMIME ($file)
  297. {
  298. // Check file exists
  299. if (!self::_Exists ($file))
  300. {
  301. return FALSE;
  302. }
  303. // Open file
  304. $finfo = finfo ();
  305. // If file could not be opened
  306. if ($finfo === FALSE)
  307. {
  308. // Set error
  309. new \Sonic\Message ('error', 'Could not get file MIME type: ' . $file);
  310. // Return FALSE
  311. return FALSE;
  312. }
  313. // Return mime type
  314. return $finfo->file ($file);
  315. }
  316. /**
  317. * Check a file exists
  318. * @param string $file File path
  319. * @return boolean
  320. */
  321. public static function _Exists ($file)
  322. {
  323. // If file doesnt exists
  324. if (!file_exists ($file))
  325. {
  326. // Set error
  327. new \Sonic\Message ('error', 'File does not exist: ' . $file);
  328. // Return FALSE
  329. return FALSE;
  330. }
  331. // Return TRUE
  332. return TRUE;
  333. }
  334. }
  335. // End File Class