file.php 2.91 KB
Newer Older
1 2 3
<?php namespace System;

class File {
Taylor Otwell committed
4

5
	/**
6
	 * Get the contents of a file.
7 8
	 *
	 * @param  string  $path
9
	 * @return string
10
	 */
11
	public static function get($path)
12
	{
13 14
		return file_get_contents($path);
	}
15

16 17 18 19 20 21 22 23 24 25 26
	/**
	 * Write to a file.
	 *
	 * @param  string  $path
	 * @param  string  $data
	 * @return int
	 */
	public static function put($path, $data)
	{
		return file_put_contents($path, $data, LOCK_EX);
	}
27

28 29 30 31 32 33 34 35 36 37
	/**
	 * Append to a file.
	 *
	 * @param  string  $path
	 * @param  string  $data
	 * @return int
	 */
	public static function append($path, $data)
	{
		return file_put_contents($path, $data, LOCK_EX | FILE_APPEND);
38 39 40
	}

	/**
41
	 * Extract the extension from a file path.
42
	 * 
Taylor Otwell committed
43
	 * @param  string  $path
44 45 46 47 48 49 50
	 * @return string
	 */
	public static function extension($path)
	{
		return pathinfo($path, PATHINFO_EXTENSION);
	}

51
	/**
52
	 * Get a file MIME type by extension.
53 54 55 56 57 58 59
	 *
	 * @param  string  $extension
	 * @param  string  $default
	 * @return string
	 */
	public static function mime($extension, $default = 'application/octet-stream')
	{
60 61 62
		$mimes = Config::get('mimes');

		if (array_key_exists($extension, $mimes))
63
		{
64
			return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
65 66 67
		}

		return $default;
68 69
	}

70
	/**
Taylor Otwell committed
71 72
	 * Determine if a file is a given type.
	 *
73 74
	 * The Fileinfo PHP extension will be used to determine the MIME type
	 * of the file. Any extension in the mimes configuration array may
Taylor Otwell committed
75 76 77 78
	 * be passed as a type.
	 */
	public static function is($extension, $path)
	{
79 80 81
		$mimes = Config::get('mimes');

		if ( ! array_key_exists($extension, $mimes))
Taylor Otwell committed
82 83 84 85 86 87
		{
			throw new \Exception("File extension [$extension] is unknown. Cannot determine file type.");
		}

		$mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);

88
		return (is_array($mimes[$extension])) ? in_array($mime, $mimes[$extension]) : $mime === $mimes[$extension];
Taylor Otwell committed
89 90 91
	}

	/**
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	 * Create a response that will force a file to be downloaded.
	 *
	 * @param  string  $path
	 * @param  string  $name
	 * @return Response
	 */
	public static function download($path, $name = null)
	{
		if (is_null($name))
		{
			$name = basename($path);
		}

		$response = Response::make(static::get($path));

		$response->header('Content-Description', 'File Transfer');
		$response->header('Content-Type', static::mime(static::extension($path)));
		$response->header('Content-Disposition', 'attachment; filename="'.$name.'"');
		$response->header('Content-Transfer-Encoding', 'binary');
		$response->header('Expires', 0);
		$response->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
		$response->header('Pragma', 'public');
		$response->header('Content-Length', filesize($path));

		return $response;
	}

119
	/**
Taylor Otwell committed
120
	 * Move an uploaded file to storage.
121
	 *
Taylor Otwell committed
122 123
	 * @param  string  $key
	 * @param  string  $path
124 125
	 * @return bool
	 */
Taylor Otwell committed
126
	public static function upload($key, $path)
127
	{
128
		return array_key_exists($key, $_FILES) ? move_uploaded_file($_FILES[$key]['tmp_name'], $path) : false;
129
	}
130
}