file.php 3.35 KB
Newer Older
Taylor Otwell committed
1
<?php namespace Laravel;
2 3

class File {
Taylor Otwell committed
4

5
	/**
6 7 8 9 10
	 * Determine if a file exists.
	 *
	 * @param  string  $path
	 * @return bool
	 */
11
	public static function exists($path)
12 13 14 15 16
	{
		return file_exists($path);
	}

	/**
17
	 * Get the contents of a file.
18 19
	 *
	 * @param  string  $path
20
	 * @return string
21
	 */
22
	public static function get($path)
23
	{
24 25
		return file_get_contents($path);
	}
26

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

39 40 41 42 43 44 45
	/**
	 * Append to a file.
	 *
	 * @param  string  $path
	 * @param  string  $data
	 * @return int
	 */
46
	public static function append($path, $data)
47 48
	{
		return file_put_contents($path, $data, LOCK_EX | FILE_APPEND);
49 50 51
	}

	/**
52 53 54 55 56
	 * Delete a file.
	 *
	 * @param  string  $path
	 * @return void
	 */
57
	public static function delete($path)
58
	{
59
		if (static::exists($path)) @unlink($path);
60 61 62
	}

	/**
Taylor Otwell committed
63
	 * Extract the file extension from a file path.
64
	 * 
Taylor Otwell committed
65
	 * @param  string  $path
66 67
	 * @return string
	 */
68
	public static function extension($path)
69 70 71 72
	{
		return pathinfo($path, PATHINFO_EXTENSION);
	}

73
	/**
74 75 76 77 78
	 * Get the file type of a given file.
	 *
	 * @param  string  $path
	 * @return string
	 */
79
	public static function type($path)
80 81 82 83 84 85 86 87 88 89
	{
		return filetype($path);
	}

	/**
	 * Get the file size of a given file.
	 *
	 * @param  string  $file
	 * @return int
	 */
90
	public static function size($path)
91 92 93 94 95 96 97 98 99 100
	{
		return filesize($path);
	}

	/**
	 * Get the file's last modification time.
	 *
	 * @param  string  $path
	 * @return int
	 */
101
	public static function modified($path)
102 103 104 105 106
	{
		return filemtime($path);
	}

	/**
107
	 * Move an uploaded file to permanent storage.
108 109 110 111 112 113
	 *
	 * @param  string  $key
	 * @param  string  $path
	 * @param  array   $files
	 * @return bool
	 */
114
	public static function upload($key, $path, $files = null)
115
	{
116 117
		if (is_null($files)) $files = $_FILES;

118 119 120 121
		return move_uploaded_file($files[$key]['tmp_name'], $path);
	}

	/**
122
	 * Get a file MIME type by extension.
123
	 *
124 125 126 127 128 129 130 131
	 * <code>
	 *		// 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');
	 * </code>
	 *
132 133 134 135
	 * @param  string  $extension
	 * @param  string  $default
	 * @return string
	 */
136
	public static function mime($extension, $default = 'application/octet-stream')
137
	{
138 139 140
		$mimes = Config::get('mimes');

		if ( ! array_key_exists($extension, $mimes)) return $default;
141

142
		return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
143 144
	}

145
	/**
Taylor Otwell committed
146 147
	 * Determine if a file is a given type.
	 *
148
	 * The Fileinfo PHP extension will be used to determine the MIME type of the file.
Taylor Otwell committed
149
	 *
150 151 152 153 154 155 156 157
	 * <code>
	 *		// 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');
	 * </code>
	 *
158 159
	 * @param  array|string  $extension
	 * @param  string        $path
Taylor Otwell committed
160
	 * @return bool
Taylor Otwell committed
161
	 */
162
	public static function is($extensions, $path)
Taylor Otwell committed
163
	{
164 165
		$mimes = Config::get('mimes');

166
		foreach ((array) $extensions as $extension)
Taylor Otwell committed
167
		{
168
			$mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
Taylor Otwell committed
169

170
			if (isset($mimes[$extension]) and in_array((array) $mimes[$extension])) return true;
171
		}
Taylor Otwell committed
172

173
		return false;
Taylor Otwell committed
174 175
	}

176
}