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

/system/helpers/upload.php

https://github.com/Toushi/flow
PHP | 162 lines | 75 code | 23 blank | 64 comment | 11 complexity | db67401304cea6e8a156de8380b854f0 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 3264 2008-08-05 19:03:14Z Geert $
  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 static 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 (Kohana::config('upload.remove_spaces') === TRUE)
  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 = Kohana::config('upload.directory', TRUE);
  41. }
  42. // Make sure the directory ends with a slash
  43. $directory = rtrim($directory, '/').'/';
  44. if ( ! is_dir($directory) AND Kohana::config('upload.create_directories') === TRUE)
  45. {
  46. // Create the upload directory
  47. mkdir($directory, 0777, TRUE);
  48. }
  49. if ( ! is_writable($directory))
  50. throw new Kohana_Exception('upload.not_writable', $directory);
  51. if (is_uploaded_file($file['tmp_name']) AND move_uploaded_file($file['tmp_name'], $filename = $directory.$filename))
  52. {
  53. if ($chmod !== FALSE)
  54. {
  55. // Set permissions on filename
  56. chmod($filename, $chmod);
  57. }
  58. // Return new file path
  59. return $filename;
  60. }
  61. return FALSE;
  62. }
  63. /* Validation Rules */
  64. /**
  65. * Tests if input data is valid file type, even if no upload is present.
  66. *
  67. * @param array $_FILES item
  68. * @return bool
  69. */
  70. public static function valid($file)
  71. {
  72. return (is_array($file)
  73. AND isset($file['error'])
  74. AND isset($file['name'])
  75. AND isset($file['type'])
  76. AND isset($file['tmp_name'])
  77. AND isset($file['size']));
  78. }
  79. /**
  80. * Tests if input data has valid upload data.
  81. *
  82. * @param array $_FILES item
  83. * @return bool
  84. */
  85. public static function required(array $file)
  86. {
  87. return (isset($file['tmp_name'])
  88. AND isset($file['error'])
  89. AND is_uploaded_file($file['tmp_name'])
  90. AND (int) $file['error'] === UPLOAD_ERR_OK);
  91. }
  92. /**
  93. * Validation rule to test if an uploaded file is allowed by extension.
  94. *
  95. * @param array $_FILES item
  96. * @param array allowed file extensions
  97. * @return bool
  98. */
  99. public static function type(array $file, array $allowed_types)
  100. {
  101. if ((int) $file['error'] !== UPLOAD_ERR_OK)
  102. return TRUE;
  103. // Get the default extension of the file
  104. $extension = strtolower(substr(strrchr($file['name'], '.'), 1));
  105. // Get the mime types for the extension
  106. $mime_types = Kohana::config('mimes.'.$extension);
  107. // Make sure there is an extension, that the extension is allowed, and that mime types exist
  108. return ( ! empty($extension) AND in_array($extension, $allowed_types) AND is_array($mime_types));
  109. }
  110. /**
  111. * Validation rule to test if an uploaded file is allowed by file size.
  112. * File sizes are defined as: SB, where S is the size (1, 15, 300, etc) and
  113. * B is the byte modifier: (B)ytes, (K)ilobytes, (M)egabytes, (G)igabytes.
  114. * Eg: to limit the size to 1MB or less, you would use "1M".
  115. *
  116. * @param array $_FILES item
  117. * @param array maximum file size
  118. * @return bool
  119. */
  120. public static function size(array $file, array $size)
  121. {
  122. if ((int) $file['error'] !== UPLOAD_ERR_OK)
  123. return TRUE;
  124. // Only one size is allowed
  125. $size = strtoupper($size[0]);
  126. if ( ! preg_match('/[0-9]++[BKMG]/', $size))
  127. return FALSE;
  128. // Make the size into a power of 1024
  129. switch (substr($size, -1))
  130. {
  131. case 'G': $size = intval($size) * pow(1024, 3); break;
  132. case 'M': $size = intval($size) * pow(1024, 2); break;
  133. case 'K': $size = intval($size) * pow(1024, 1); break;
  134. default: $size = intval($size); break;
  135. }
  136. // Test that the file is under or equal to the max size
  137. return ($file['size'] <= $size);
  138. }
  139. } // End upload