$directory)); if (is_uploaded_file($file['tmp_name']) AND move_uploaded_file($file['tmp_name'], $filename = $directory.$filename)) { if ($chmod !== FALSE) { // Set permissions on filename chmod($filename, $chmod); } // Return new file path return $filename; } return FALSE; } /* Validation Rules */ /** * Tests if input data is valid file type, even if no upload is present. * * @param array $_FILES item * @return bool */ public static function valid($file) { return (is_array($file) AND isset($file['error']) AND isset($file['name']) AND isset($file['type']) AND isset($file['tmp_name']) AND isset($file['size'])); } /** * Tests if input data has valid upload data. * * @param array $_FILES item * @return bool */ public static function required(array $file) { return (isset($file['tmp_name']) AND isset($file['error']) AND is_uploaded_file($file['tmp_name']) AND (int) $file['error'] === UPLOAD_ERR_OK); } /** * Validation rule to test if an uploaded file is allowed by extension. * * @param array $_FILES item * @param array allowed file extensions * @return bool */ public static function type(array $file, array $allowed_types) { if ((int) $file['error'] !== UPLOAD_ERR_OK) return TRUE; // Get the default extension of the file $extension = strtolower(substr(strrchr($file['name'], '.'), 1)); // Make sure there is an extension and that the extension is allowed return ( ! empty($extension) AND in_array($extension, $allowed_types)); } /** * 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. * Eg: to limit the size to 1MB or less, you would use "1M". * * @param array $_FILES item * @param array maximum file size * @return bool */ public static function size(array $file, array $size) { if ((int) $file['error'] !== UPLOAD_ERR_OK) return TRUE; // Only one size is allowed $size = strtoupper($size[0]); if ( ! preg_match('/[0-9]++[BKMG]/', $size)) return FALSE; // 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); } } // End upload