PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/upload.php

https://github.com/jpvf/boilerplate
PHP | 134 lines | 109 code | 25 blank | 0 comment | 21 complexity | 309934f9b80daeeecf2fe6a48d2df143 MD5 | raw file
  1. <?php
  2. class upload{
  3. private static $upload;
  4. var $file = null;
  5. public function __construct()
  6. {
  7. if(isset($_FILES) && !empty($_FILES)){
  8. $this->file = $_FILES['file'];
  9. }
  10. }
  11. public static function getInstance()
  12. {
  13. if (!self::$upload)
  14. {
  15. self::$upload = new upload();
  16. }
  17. return self::$upload;
  18. }
  19. private function _check_ext()
  20. {
  21. $this->allowed_ext = explode('|', $this->allowed);
  22. $this->file_ext = strrchr($this->file['name'],'.');
  23. list($dot, $this->file_ext) = explode('.', $this->file_ext);
  24. foreach($this->allowed_ext as $key => $val){
  25. if($val == $this->file_ext){
  26. return true;
  27. }
  28. }
  29. return false;
  30. }
  31. private function _check_size()
  32. {
  33. $this->file_size = $this->file['size'];
  34. if($this->max_size >= $this->file['size']){
  35. return true;
  36. }
  37. return false;
  38. }
  39. private function _move()
  40. {
  41. $this->file_ext = ($this->file_ext == 'jpg') ? ('jpeg') : ($this->file_ext);
  42. $this->base_name = $this->file['name'];
  43. if(isset($this->file_name) && !empty($this->file_name)){
  44. $this->file['name'] = $this->file_name . '.' .$this->file_ext;
  45. }
  46. if(isset($this->timestamp) && !empty($this->timestamp) && $this->timestamp === TRUE){
  47. $ext = strrchr($this->file['name'],'.');
  48. $name = reverse_strrchr($this->file['name'],'.');
  49. list($dot, $ext) = explode('.', $ext);
  50. $ext = ($ext == 'jpg') ? ('jpeg') : ($ext);
  51. $this->timestamp_val = date('YmdHis');
  52. $this->file['name'] = $name . '-' . $this->timestamp_val . '.' . $ext;
  53. }
  54. if(move_uploaded_file($this->file['tmp_name'], $this->upload_path . $this->file['name'] )) {
  55. return true;
  56. }
  57. return false;
  58. }
  59. public function do_upload($config = array())
  60. {
  61. if(!$this->file){
  62. return;
  63. }
  64. $this->_setDefaults($config);
  65. if(!$this->_check_ext()){
  66. return false;
  67. }
  68. if(!$this->_check_size()){
  69. return false;
  70. }
  71. if(file_exists($this->upload_path . $this->file['name'])){
  72. return false;
  73. }
  74. if(!$this->_move()){
  75. return false;
  76. }
  77. return true;
  78. }
  79. private function _setDefaults($upload_config = array())
  80. {
  81. include_once(RUTA_CONFIG . 'upload' . EXT);
  82. if(count($config) > 0){
  83. foreach($config as $key => $val){
  84. $this->$key = $val;
  85. }
  86. }
  87. if(count($upload_config) > 0){
  88. foreach($upload_config as $key => $val){
  89. $this->$key = $val;
  90. }
  91. }
  92. }
  93. public function get_name()
  94. {
  95. return $this->upload_path . $this->file['name'];
  96. }
  97. public function get_size()
  98. {
  99. return $this->file_size;
  100. }
  101. function get_base_filename()
  102. {
  103. return $this->base_name;
  104. }
  105. public function get_timestamp()
  106. {
  107. return $this->timestamp_val;
  108. }
  109. }