This is documentation for Kohana v2.3.x.

Table of Contents
Statusstub
TodoWrite me

Upload Helper

The upload helper is designed to work with the global $_FILES array and validation library. More information about PHP file uploads.

Configuration

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;

Upload directory

$config['directory'] sets the path to the saved files. This path is relative to your index file. Absolute paths are also supported.

Directory creation

$config['create_directories'] enable or disable directory creation.

Remove spaces

$config['remove_spaces'] removes spaces from uploaded filenames.

Complete example

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);
    }

Methods

save()

save($file, $filename = NULL, $directory = NULL, $chmod = 0644) saves an uploaded file to a new location. It takes:

valid()

valid($file) tests if input data is valid file type, even if no upload is present.

required()

required(array $file) tests if input data has valid upload data.

type()

type(array $file, array $allowed_types) tests if an uploaded file is allowed by extension.

size()

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”.