This is documentation for Kohana v2.3.x.
Status | stub |
---|---|
Todo | Write me |
The upload helper is designed to work with the global $_FILES array and validation library. More information about PHP file uploads.
Configuration is done in the application/config/upload.php
file, if it's not there take the one from system/config
and copy it to the application folder (see cascading filesystem):
$config['directory'] = DOCROOT.'upload'; $config['create_directories'] = FALSE; $config['remove_spaces'] = TRUE;
$config['directory']
sets the path to the saved files. This path is relative to your index file. Absolute paths are also supported.
$config['create_directories']
enable or disable directory creation.
$config['remove_spaces']
removes spaces from uploaded filenames.
The example below demonstrates how to validate a file upload (where the field name is `picture`), save it temporarily and apply some image manipulation.
$files = Validation::factory($_FILES) ->add_rules('picture', 'upload::valid', 'upload::required', 'upload::type[gif,jpg,png]', 'upload::size[1M]'); if ($files->validate()) { // Temporary file name $filename = upload::save('picture'); // Resize, sharpen, and save the image Image::factory($filename) ->resize(100, 100, Image::WIDTH) ->save(DOCROOT.'media/pictures/'.basename($filename)); // Remove the temporary file unlink($filename); }
The example below demonstrates how to upload several images
foreach( arr::rotate($_FILES['image']) as $file ) { $filename = upload::save($file); Image::factory($filename) ->resize(30, 30, Image::AUTO) ->save(DOCROOT.'upload/'.basename($filename)); unlink($filename); }
save($file, $filename = NULL, $directory = NULL, $chmod = 0644)
saves an uploaded file to a new location. It takes:
$file
name of $_FILE input or array of upload data$filename
new filename, if omitted use the default filename, with a timestamp pre-pended$directory
new directory, if omitted uses the pre-configured upload directory$chmod
chmod mask, default 0644
valid($file)
tests if input data is valid file type, even if no upload is present.
$file
$_FILES items
required(array $file)
tests if input data has valid upload data.
$file
$_FILES items
type(array $file, array $allowed_types)
tests if an uploaded file is allowed by extension.
$file
$_FILES items$allowed_types
allowed file extensions
size(array $file, array $size)
tests if an uploaded file is allowed by file size. File sizes are defined as: SB, where S is the size (1, 15, 300, etc) and B is the byte modifier: (B)ytes, (K)ilobytes, (M)egabytes, (G)igabytes. Eg: to limit the size to 1MB or less, you would use “1M”.
$file
$_FILES items$size
maximum file size