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 an file upload (a picture), save it temporary and apply some image manipulation.

$_FILES = Validation::factory($_FILES)
	->add_rules('picture', 'upload::valid', '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).'.jpg');
 
	// Remove the temporary file
	unlink($filename);
}

Methods

Saving the uploaded file

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

Validation rules

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

helpers/upload.txt · Last modified: 2008/08/24 13:52 by neovive