Advertisement
Guest User

Untitled

a guest
Feb 6th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. public function imageUpload($input_file_name = 'image', $dest_filename)
  2. {
  3. if (isset($_FILES[$input_file_name])) {
  4. if (!empty($_FILES[$input_file_name]['error'])) {
  5. $upload_error_code = $_FILES[$input_file_name]['error'];
  6. if ($upload_error_code == 4) {
  7. return false; // file not loaded (probably not choosen)
  8. } else {
  9. throw new Exception(
  10. $this->fileUploadCodeToMessage($upload_error_code));
  11. }
  12. } else {
  13. $uploaded = $_FILES[$input_file_name]['tmp_name'];
  14. if (!is_uploaded_file($uploaded)) {
  15. throw new Exception(
  16. "Possible file upload attack: ".$uploaded);
  17. }
  18. if (!file_exists($uploaded)) {
  19. throw new Exception( "File: ".$uploaded." not found.");
  20. }
  21. // is image?
  22. if (!$size = getimagesize($uploaded)){
  23. throw new Exception('Unsupported file format');
  24. }
  25. $dirname = dirname($dest_filename);
  26. if (!is_dir($dirname)) {
  27. // full for owner, read and exec(?) for others, recursive creation
  28. mkdir($dirname, 0755, true);
  29. }
  30. if (move_uploaded_file($uploaded, $dest_filename)) {
  31. chmod($dest_filename, 0644); // RW for owner, read for everybody else
  32. return true;
  33. } else {
  34. throw new Exception(
  35. "Can't move uploaded file: "
  36. ."\nfilesize=".filesize($uploaded)
  37. ."\ndisk_free_space=".disk_free_space($dirname));
  38. }
  39. }
  40. } else return false;
  41. }
  42. public function fileUploadCodeToMessage($code) {
  43. switch ($code) {
  44. case UPLOAD_ERR_INI_SIZE:
  45. return "php.ini upload_max_filesize exceeded";
  46. case UPLOAD_ERR_FORM_SIZE:
  47. return "HTML form MAX_FILE_SIZE exceeded";
  48. case UPLOAD_ERR_PARTIAL:
  49. return "The uploaded file was only partially uploaded";
  50. case UPLOAD_ERR_NO_FILE:
  51. return "No file was attached";
  52. case UPLOAD_ERR_NO_TMP_DIR:
  53. return "Missing a temporary folder";
  54. case UPLOAD_ERR_CANT_WRITE:
  55. return "Failed to write file to disk";
  56. case UPLOAD_ERR_EXTENSION:
  57. return "File upload stopped by extension";
  58. default:
  59. return "Unknown upload error (code $code)";
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement