Upload
› Kohana_Upload
Class Contents
Constants
- None
Properties
Methods
Class declared in SYSPATH/classes/upload.php on line 3.
Properties
- public static
$default_directory string(6) "upload"- public static
$remove_spaces bool TRUE
Methods
public static not_empty( )
› Kohana_Upload
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( )
› Kohana_Upload
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+/', '_', $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( )
› Kohana_Upload
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( )
› Kohana_Upload
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( )
› Kohana_Upload
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']));
}