1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
3 * Upload helper class for working with the global $_FILES
4 * array and Validation library.
6 * $Id: upload.php 3917 2009-01-21 03:06:22Z zombor $
10 * @copyright (c) 2007-2008 Kohana Team
11 * @license http://kohanaphp.com/license.html
16 * Save an uploaded file to a new location.
18 * @param mixed name of $_FILE input or array of upload data
19 * @param string new filename
20 * @param string new directory
21 * @param integer chmod mask
22 * @return string full path to new file
24 public static function save($file, $filename = NULL, $directory = NULL, $chmod = 0644)
26 // Load file data from FILES if not passed as array
27 $file = is_array($file) ?
$file : $_FILES[$file];
29 if ($filename === NULL)
31 // Use the default filename, with a timestamp pre-pended
32 $filename = time().$file['name'];
35 if (Kohana
::config('upload.remove_spaces') === TRUE)
37 // Remove spaces from the filename
38 $filename = preg_replace('/\s+/', '_', $filename);
41 if ($directory === NULL)
43 // Use the pre-configured upload directory
44 $directory = Kohana
::config('upload.directory', TRUE);
47 // Make sure the directory ends with a slash
48 $directory = rtrim($directory, '/').'/';
50 if ( ! is_dir($directory) AND Kohana
::config('upload.create_directories') === TRUE)
52 // Create the upload directory
53 mkdir($directory, 0777, TRUE);
56 if ( ! is_writable($directory))
57 throw new Kohana_Exception('upload.not_writable', $directory);
59 if (is_uploaded_file($file['tmp_name']) AND move_uploaded_file($file['tmp_name'], $filename = $directory.$filename))
63 // Set permissions on filename
64 chmod($filename, $chmod);
67 // Return new file path
74 /* Validation Rules */
77 * Tests if input data is valid file type, even if no upload is present.
79 * @param array $_FILES item
82 public static function valid($file)
84 return (is_array($file)
85 AND isset($file['error'])
86 AND isset($file['name'])
87 AND isset($file['type'])
88 AND isset($file['tmp_name'])
89 AND isset($file['size']));
93 * Tests if input data has valid upload data.
95 * @param array $_FILES item
98 public static function required(array $file)
100 return (isset($file['tmp_name'])
101 AND isset($file['error'])
102 AND is_uploaded_file($file['tmp_name'])
103 AND (int) $file['error'] === UPLOAD_ERR_OK
);
107 * Validation rule to test if an uploaded file is allowed by extension.
109 * @param array $_FILES item
110 * @param array allowed file extensions
113 public static function type(array $file, array $allowed_types)
115 if ((int) $file['error'] !== UPLOAD_ERR_OK
)
118 // Get the default extension of the file
119 $extension = strtolower(substr(strrchr($file['name'], '.'), 1));
121 // Get the mime types for the extension
122 $mime_types = Kohana
::config('mimes.'.$extension);
124 // Make sure there is an extension, that the extension is allowed, and that mime types exist
125 return ( ! empty($extension) AND in_array($extension, $allowed_types) AND is_array($mime_types));
129 * Validation rule to test if an uploaded file is allowed by file size.
130 * File sizes are defined as: SB, where S is the size (1, 15, 300, etc) and
131 * B is the byte modifier: (B)ytes, (K)ilobytes, (M)egabytes, (G)igabytes.
132 * Eg: to limit the size to 1MB or less, you would use "1M".
134 * @param array $_FILES item
135 * @param array maximum file size
138 public static function size(array $file, array $size)
140 if ((int) $file['error'] !== UPLOAD_ERR_OK
)
143 // Only one size is allowed
144 $size = strtoupper($size[0]);
146 if ( ! preg_match('/[0-9]++[BKMG]/', $size))
149 // Make the size into a power of 1024
150 switch (substr($size, -1))
152 case 'G': $size = intval($size) * pow(1024, 3); break;
153 case 'M': $size = intval($size) * pow(1024, 2); break;
154 case 'K': $size = intval($size) * pow(1024, 1); break;
155 default: $size = intval($size); break;
158 // Test that the file is under or equal to the max size
159 return ($file['size'] <= $size);