PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/system/helpers/upload.php

https://github.com/MHordecki/milionkostek
PHP | 156 lines | 71 code | 22 blank | 63 comment | 10 complexity | dc32adc1f5d426dad1bf6ca20db7a819 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Upload helper class for working with the global $_FILES
  4. * array and Validation library.
  5. *
  6. * $Id: upload.php 2849 2008-06-16 20:27:20Z Shadowhand $
  7. *
  8. * @package Core
  9. * @author Kohana Team
  10. * @copyright (c) 2007-2008 Kohana Team
  11. * @license http://kohanaphp.com/license.html
  12. */
  13. class upload_Core {
  14. /**
  15. * Save an uploaded file to a new location.
  16. *
  17. * @param mixed name of $_FILE input or array of upload data
  18. * @param string new filename
  19. * @param string new directory
  20. * @param integer chmod mask
  21. * @return string full path to new file
  22. */
  23. public function save($file, $filename = NULL, $directory = NULL, $chmod = 0644)
  24. {
  25. // Load file data from FILES if not passed as array
  26. $file = is_array($file) ? $file : $_FILES[$file];
  27. if ($filename === NULL)
  28. {
  29. // Use the default filename, with a timestamp pre-pended
  30. $filename = time().$file['name'];
  31. }
  32. if (Config::item('upload.remove_spaces'))
  33. {
  34. // Remove spaces from the filename
  35. $filename = preg_replace('/\s+/', '_', $filename);
  36. }
  37. if ($directory === NULL)
  38. {
  39. // Use the pre-configured upload directory
  40. $directory = Config::item('upload.directory', TRUE);
  41. }
  42. // Make sure the directory ends with a slash
  43. $directory = rtrim($directory, '/').'/';
  44. if ( ! is_writable($directory))
  45. throw new Kohana_Exception('upload.not_writable', $directory);
  46. if (is_uploaded_file($file['tmp_name']) AND move_uploaded_file($file['tmp_name'], $filename = $directory.$filename))
  47. {
  48. if ($chmod !== FALSE)
  49. {
  50. // Set permissions on filename
  51. chmod($filename, $chmod);
  52. }
  53. // Return new file path
  54. return $filename;
  55. }
  56. return FALSE;
  57. }
  58. /* Validation Rules */
  59. /**
  60. * Tests if a $_FILES item exists.
  61. *
  62. * @param array $_FILES item
  63. * @return bool
  64. */
  65. public static function required($file)
  66. {
  67. return (is_array($file)
  68. AND isset($file['error'])
  69. AND isset($file['name'])
  70. AND isset($file['type'])
  71. AND isset($file['tmp_name'])
  72. AND isset($file['size']));
  73. }
  74. /**
  75. * Tests if a $_FILES item is valid.
  76. *
  77. * @param array $_FILES item
  78. * @return bool
  79. */
  80. public static function valid(array $file)
  81. {
  82. return (isset($file['tmp_name'])
  83. AND isset($file['error'])
  84. AND is_uploaded_file($file['tmp_name'])
  85. AND (int) $file['error'] === UPLOAD_ERR_OK);
  86. }
  87. /**
  88. * Validation rule to test if an uploaded file is allowed by extension.
  89. *
  90. * @param array $_FILES item
  91. * @param array allowed file extensions
  92. * @return bool
  93. */
  94. public static function type(array $file, array $allowed_types)
  95. {
  96. if ((int) $file['error'] !== UPLOAD_ERR_OK)
  97. return TRUE;
  98. // Get the default extension of the file
  99. $extension = strtolower(file::extension($file['name']));
  100. // Get the mime types for the extension
  101. $mime_types = Config::item('mimes.'.$extension);
  102. // Make sure there is an extension, that the extension is allowed, and that mime types exist
  103. return ( ! empty($extension) AND in_array($extension, $allowed_types) AND is_array($mime_types));
  104. }
  105. /**
  106. * Validation rule to test if an uploaded file is allowed by file size.
  107. * File sizes are defined as: SB, where S is the size (1, 15, 300, etc) and
  108. * B is the byte modifier: (B)ytes, (K)ilobytes, (M)egabytes, (G)igabytes.
  109. * Eg: to limit the size to 1MB or less, you would use "1M".
  110. *
  111. * @param array $_FILES item
  112. * @param array maximum file size
  113. * @return bool
  114. */
  115. public function size(array $file, array $size)
  116. {
  117. if ((int) $file['error'] !== UPLOAD_ERR_OK)
  118. return TRUE;
  119. // Only one size is allowed
  120. $size = strtoupper($size[0]);
  121. if ( ! preg_match('/[0-9]+[BKMG]/', $size))
  122. return FALSE;
  123. // Make the size into a power of 1024
  124. switch (substr($size, -1))
  125. {
  126. case 'G': $size = intval($size) * pow(1024, 3); break;
  127. case 'M': $size = intval($size) * pow(1024, 2); break;
  128. case 'K': $size = intval($size) * pow(1024, 1); break;
  129. default: $size = intval($size); break;
  130. }
  131. // Test that the file is under or equal to the max size
  132. return ($file['size'] <= $size);
  133. }
  134. } // End upload