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

/application/libraries/Multi_upload.php

https://bitbucket.org/matyhaty/senses-designertravelv3
PHP | 286 lines | 174 code | 36 blank | 76 comment | 30 complexity | c0ac4f4c886e691b600999ba67c5c813 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * This library assumes that you have already loaded the default CI Upload Library seperately
  4. *
  5. * Functions is based upon CI_Upload, Feel free to modify this
  6. * library to function as an extension to CI_Upload
  7. *
  8. * Library modified by: Alvin Mites
  9. * http://www.mitesdesign.com
  10. *
  11. */
  12. class Multi_upload {
  13. function Multi_upload () {
  14. // $CI =& get_instance();
  15. }
  16. /**
  17. * Perform multiple file uploads
  18. * Based upon JQuery Multiple Upload Class
  19. * see http://www.fyneworks.com/jquery/multiple-file-upload/
  20. */
  21. function go_upload($field = 'userfile') {
  22. $CI =& get_instance();
  23. // Is $_FILES[$field] set? If not, no reason to continue.
  24. if ( ! isset($_FILES[$field]['name'][0]))
  25. {
  26. $CI->upload->set_error('upload_no_file_selected');
  27. return FALSE;
  28. } else
  29. {
  30. $num_files = count($_FILES[$field]['name']) -1;
  31. $file_list = array();
  32. $error_hold = array();
  33. $error_upload = FALSE;
  34. }
  35. // Is the upload path valid?
  36. if ( ! $CI->upload->validate_upload_path())
  37. {
  38. // errors will already be set by validate_upload_path() so just return FALSE
  39. return FALSE;
  40. }
  41. for ($i=0; $i < $num_files; $i++) {
  42. // $fname = $_FILES[$field]['name'][$i];
  43. // echo "$fname\n\n<br><br>\n\n";
  44. $error_hold[$i] = FALSE;
  45. // Was the file able to be uploaded? If not, determine the reason why.
  46. if ( ! is_uploaded_file($_FILES[$field]['tmp_name'][$i]))
  47. {
  48. $error = ( ! isset($_FILES[$field]['error'][$i])) ? 4 : $_FILES[$field]['error'][$i];
  49. switch($error)
  50. {
  51. case 1: // UPLOAD_ERR_INI_SIZE
  52. $error_hold[$i] = 'upload_file_exceeds_limit';
  53. break;
  54. case 2: // UPLOAD_ERR_FORM_SIZE
  55. $error_hold[$i] = 'upload_file_exceeds_form_limit';
  56. break;
  57. case 3: // UPLOAD_ERR_PARTIAL
  58. $error_hold[$i] = 'upload_file_partial';
  59. break;
  60. case 4: // UPLOAD_ERR_NO_FILE
  61. $error_hold[$i] = 'upload_no_file_selected';
  62. break;
  63. case 6: // UPLOAD_ERR_NO_TMP_DIR
  64. $error_hold[$i] = 'upload_no_temp_directory';
  65. break;
  66. case 7: // UPLOAD_ERR_CANT_WRITE
  67. $error_hold[$i] = 'upload_unable_to_write_file';
  68. break;
  69. case 8: // UPLOAD_ERR_EXTENSION
  70. $error_hold[$i] = 'upload_stopped_by_extension';
  71. break;
  72. default :
  73. $error_hold[$i] = 'upload_no_file_selected';
  74. break;
  75. }
  76. return FALSE;
  77. }
  78. // Set the uploaded data as class variables
  79. $CI->upload->file_temp = $_FILES[$field]['tmp_name'][$i];
  80. $CI->upload->file_name = $CI->upload->_prep_filename($_FILES[$field]['name'][$i]);
  81. $CI->upload->file_size = $_FILES[$field]['size'][$i];
  82. $CI->upload->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type'][$i]);
  83. $CI->upload->file_type = strtolower($CI->upload->file_type);
  84. $CI->upload->file_ext = $CI->upload->get_extension($_FILES[$field]['name'][$i]);
  85. // Convert the file size to kilobytes
  86. if ($CI->upload->file_size > 0)
  87. {
  88. $CI->upload->file_size = round($CI->upload->file_size/1024, 2);
  89. }
  90. // Is the file type allowed to be uploaded?
  91. if ( ! $CI->upload->is_allowed_filetype())
  92. {
  93. $error_hold[$i] = 'upload_invalid_filetype';
  94. }
  95. // Is the file size within the allowed maximum?
  96. if ( ! $CI->upload->is_allowed_filesize())
  97. {
  98. $error_hold[$i] = 'upload_invalid_filesize';
  99. }
  100. // Are the image dimensions within the allowed size?
  101. // Note: This can fail if the server has an open_basdir restriction.
  102. if ( ! $CI->upload->is_allowed_dimensions())
  103. {
  104. $error_hold[$i] = 'upload_invalid_dimensions';
  105. }
  106. // Sanitize the file name for security
  107. $CI->upload->file_name = $CI->upload->clean_file_name($CI->upload->file_name);
  108. // Remove white spaces in the name
  109. if ($CI->upload->remove_spaces == TRUE)
  110. {
  111. $CI->upload->file_name = preg_replace("/\s+/", "_", $CI->upload->file_name);
  112. }
  113. /*
  114. * Validate the file name
  115. * This function appends an number onto the end of
  116. * the file if one with the same name already exists.
  117. * If it returns false there was a problem.
  118. */
  119. $CI->upload->orig_name = $CI->upload->file_name;
  120. if ($CI->upload->overwrite == FALSE)
  121. {
  122. $CI->upload->file_name = $CI->upload->set_filename($CI->upload->upload_path, $CI->upload->file_name);
  123. if ($CI->upload->file_name === FALSE)
  124. {
  125. $error_hold[$i] = TRUE;
  126. }
  127. }
  128. /*
  129. * Move the file to the final destination
  130. * To deal with different server configurations
  131. * we'll attempt to use copy() first. If that fails
  132. * we'll use move_uploaded_file(). One of the two should
  133. * reliably work in most environments
  134. */
  135. if ( ! @copy($CI->upload->file_temp, $CI->upload->upload_path.$CI->upload->file_name))
  136. {
  137. if ( ! @move_uploaded_file($CI->upload->file_temp, $CI->upload->upload_path.$CI->upload->file_name))
  138. {
  139. $error_hold[$i] = 'upload_destination_error';
  140. }
  141. }
  142. /*
  143. * Run the file through the XSS hacking filter
  144. * This helps prevent malicious code from being
  145. * embedded within a file. Scripts can easily
  146. * be disguised as images or other file types.
  147. */
  148. if ($CI->upload->xss_clean == TRUE)
  149. {
  150. $CI->upload->do_xss_clean();
  151. }
  152. if ($error_hold[$i]) {
  153. $error_upload = TRUE;
  154. // echo $error_hold[$i];
  155. } else {
  156. if ($imageVar = $this->multiple_image_properties($CI->upload->upload_path.$CI->upload->file_name)) {
  157. $file_list[] = array(
  158. 'name' => $CI->upload->file_name,
  159. 'file' => $CI->upload->upload_path.$CI->upload->file_name,
  160. 'size' => $CI->upload->file_size,
  161. 'ext' => $CI->upload->file_ext,
  162. 'image_type' => $imageVar->image_type,
  163. 'height' => $imageVar->height,
  164. 'width' => $imageVar->width
  165. );
  166. } else {
  167. $file_list[] = array(
  168. 'name' => $CI->upload->file_name,
  169. 'file' => $CI->upload->upload_path.$CI->upload->file_name,
  170. 'size' => $CI->upload->file_size,
  171. 'type' => $CI->upload->file_type,
  172. 'ext' => $CI->upload->file_ext,
  173. );
  174. }
  175. }
  176. // For debugging
  177. /*
  178. if (strlen($error_hold[$i]) > 1) {
  179. print_r($error_hold);
  180. }
  181. */
  182. } // end for loop
  183. // Add error display for individual files
  184. if ($error_upload) {
  185. $this->set_error($error_hold);
  186. return FALSE;
  187. } else {
  188. return $file_list;
  189. }
  190. }
  191. // --------------------------------------------------------------------
  192. /**
  193. * Set Image Properties
  194. *
  195. * Uses GD to determine the width/height/type of image
  196. *
  197. * @access public
  198. * @param string
  199. * @return void
  200. */
  201. function multiple_image_properties($path = '')
  202. {
  203. $CI =& get_instance();
  204. if ( ! $CI->upload->is_image())
  205. {
  206. return false;
  207. }
  208. if (function_exists('getimagesize'))
  209. {
  210. if (FALSE !== ($D = @getimagesize($path)))
  211. {
  212. $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
  213. $image->width = $D['0'];
  214. $image->height = $D['1'];
  215. $image->image_type = ( ! isset($types[$D['2']])) ? 'unknown' : $types[$D['2']];
  216. return $image;
  217. }
  218. }
  219. }
  220. // --------------------------------------------------------------------
  221. /**
  222. * Set an error message
  223. *
  224. * @access public
  225. * @param string
  226. * @return void
  227. */
  228. function set_error($msg)
  229. {
  230. $CI =& get_instance();
  231. $CI->lang->load('upload');
  232. if (is_array($msg))
  233. {
  234. foreach ($msg as $val)
  235. {
  236. $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
  237. $this->error_msg[] = $msg;
  238. log_message('error', $msg);
  239. }
  240. }
  241. else
  242. {
  243. $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
  244. $this->error_msg[] = $msg;
  245. log_message('error', $msg);
  246. }
  247. }
  248. // --------------------------------------------------------------------
  249. }
  250. ?>