* // Upload the $_FILES['photo'] file to a permanent location * File::upload('photo', 'path/to/new/home.jpg'); * * * @param string $key * @param string $path * @return bool */ public static function upload($key, $path) { if ( ! isset($_FILES[$key])) return false; return move_uploaded_file($_FILES[$key]['tmp_name'], $path); } /** * Get a file MIME type by extension. * * * // Determine the MIME type for the .tar extension * $mime = File::mime('tar'); * * // Return a default value if the MIME can't be determined * $mime = File::mime('ext', 'application/octet-stream'); * * * @param string $extension * @param string $default * @return string */ public static function mime($extension, $default = 'application/octet-stream') { $mimes = Config::get('mimes'); if ( ! array_key_exists($extension, $mimes)) return $default; return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension]; } /** * Determine if a file is a given type. * * The Fileinfo PHP extension will be used to determine the MIME type of the file. * * * // Determine if a file is a JPG image * $jpg = File::is('jpg', 'path/to/file.jpg'); * * // Determine if a file is one of a given list of types * $image = File::is(array('jpg', 'png', 'gif'), 'path/to/file'); * * * @param array|string $extension * @param string $path * @return bool */ public static function is($extensions, $path) { $mimes = Config::get('mimes'); foreach ((array) $extensions as $extension) { $mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path); if (isset($mimes[$extension]) and in_array($mime, (array) $mimes[$extension])) { return true; } } return false; } }