Modules

Upload
extends Kohana_Upload

Upload helper class for working with uploaded files and Validate.

$array = Validate::factory($_FILES);

Remember to define your form with "enctype=multipart/form-data" or file uploading will not work!

The following configuration properties can be set:

package
Kohana
category
Helpers
author
Kohana Team
copyright
© 2007-2010 Kohana Team
license
http://kohanaframework.org/license

Class declared in SYSPATH/classes/upload.php on line 3.

Properties

public static string $default_directory

default upload directory

string(6) "upload"

public static boolean $remove_spaces

remove spaces in uploaded files

bool TRUE

Methods

public static not_empty( array $file ) (defined in Kohana_Upload)

Tests if a successful upload has been made.

$array->rule('file', 'Upload::not_empty');

Parameters

  • array $file required - $_FILES item

Return Values

  • bool

Source Code

public static function not_empty(array $file)
{
	return (isset($file['error'])
		AND isset($file['tmp_name'])
		AND $file['error'] === UPLOAD_ERR_OK
		AND is_uploaded_file($file['tmp_name'])
	);
}

public static save( array $file [, string $filename = NULL , string $directory = NULL , integer $chmod = integer 420 ] ) (defined in Kohana_Upload)

Save an uploaded file to a new location. If no filename is provided, the original filename will be used, with a unique prefix added.

This method should be used after validating the $_FILES array:

if ($array->check())
{
    // Upload is valid, save it
    Upload::save($_FILES['file']);
}

Parameters

  • array $file required - Uploaded file data
  • string $filename = NULL - New filename
  • string $directory = NULL - New directory
  • integer $chmod = integer 420 - Chmod mask

Return Values

  • string - On success, full path to new file
  • FALSE - On failure

Source Code

public static function save(array $file, $filename = NULL, $directory = NULL, $chmod = 0644)
{
	if ( ! isset($file['tmp_name']) OR ! is_uploaded_file($file['tmp_name']))
	{
		// Ignore corrupted uploads
		return FALSE;
	}

	if ($filename === NULL)
	{
		// Use the default filename, with a timestamp pre-pended
		$filename = uniqid().$file['name'];
	}

	if (Upload::$remove_spaces === TRUE)
	{
		// Remove spaces from the filename
		$filename = preg_replace('/\s+/u', '_', $filename);
	}

	if ($directory === NULL)
	{
		// Use the pre-configured upload directory
		$directory = Upload::$default_directory;
	}

	if ( ! is_dir($directory) OR ! is_writable(realpath($directory)))
	{
		throw new Kohana_Exception('Directory :dir must be writable',
			array(':dir' => Kohana::debug_path($directory)));
	}

	// Make the filename into a complete path
	$filename = realpath($directory).DIRECTORY_SEPARATOR.$filename;

	if (move_uploaded_file($file['tmp_name'], $filename))
	{
		if ($chmod !== FALSE)
		{
			// Set permissions on filename
			chmod($filename, $chmod);
		}

		// Return new file path
		return $filename;
	}

	return FALSE;
}

public static size( array $file , string $size ) (defined in Kohana_Upload)

Validation rule to test 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.

$array->rule('file', 'Upload::size', array('1M'))

Parameters

  • array $file required - $_FILES item
  • string $size required - Maximum file size

Return Values

  • bool

Source Code

public static function size(array $file, $size)
{
	if ($file['error'] === UPLOAD_ERR_INI_SIZE)
	{
		// Upload is larger than PHP allowed size
		return FALSE;
	}

	if ($file['error'] !== UPLOAD_ERR_OK)
	{
		// The upload failed, no size to check
		return TRUE;
	}

	// Only one size is allowed
	$size = strtoupper(trim($size));

	if ( ! preg_match('/^[0-9]++[BKMG]$/', $size))
	{
		throw new Kohana_Exception('Size does not contain a digit and a byte value: :size', array(
			':size' => $size,
		));
	}

	// Make the size into a power of 1024
	switch (substr($size, -1))
	{
		case 'G':
			$size = intval($size) * pow(1024, 3);
		break;
		case 'M':
			$size = intval($size) * pow(1024, 2);
		break;
		case 'K':
			$size = intval($size) * pow(1024, 1);
		break;
		default:
			$size = intval($size);
		break;
	}

	// Test that the file is under or equal to the max size
	return ($file['size'] <= $size);
}

public static type( array $file , array $allowed ) (defined in Kohana_Upload)

Test if an uploaded file is an allowed file type, by extension.

$array->rule('file', 'Upload::type', array(array('jpg', 'png', 'gif')));

Parameters

  • array $file required - $_FILES item
  • array $allowed required - Allowed file extensions

Return Values

  • bool

Source Code

public static function type(array $file, array $allowed)
{
	if ($file['error'] !== UPLOAD_ERR_OK)
		return TRUE;

	$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));

	return in_array($ext, $allowed);
}

public static valid( array $file ) (defined in Kohana_Upload)

Tests if upload data is valid, even if no file was uploaded. If you do require a file to be uploaded, add the Upload::not_empty rule before this rule.

$array->rule('file', 'Upload::valid')

Parameters

  • array $file required - $_FILES item

Return Values

  • bool

Source Code

public static function valid($file)
{
	return (isset($file['error'])
		AND isset($file['name'])
		AND isset($file['type'])
		AND isset($file['tmp_name'])
		AND isset($file['size']));
}