file.php 6.7 KB
Newer Older
1
<?php namespace Laravel; use Closure, FilesystemIterator as fIterator;
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 20
	 * <code>
	 *		// Get the contents of a file
Taylor Otwell committed
21
	 *		$contents = File::get(path('app').'routes'.EXT);
22 23
	 *
	 *		// Get the contents of a file or return a default value if it doesn't exist
Taylor Otwell committed
24
	 *		$contents = File::get(path('app').'routes'.EXT, 'Default Value');
25 26
	 * </code>
	 *
27
	 * @param  string  $path
28
	 * @param  mixed   $default
29
	 * @return string
30
	 */
31
	public static function get($path, $default = null)
32
	{
33
		return (file_exists($path)) ? file_get_contents($path) : value($default);
34
	}
35

36 37 38 39 40 41 42
	/**
	 * Write to a file.
	 *
	 * @param  string  $path
	 * @param  string  $data
	 * @return int
	 */
43
	public static function put($path, $data)
44 45 46
	{
		return file_put_contents($path, $data, LOCK_EX);
	}
47

48 49 50 51 52 53 54
	/**
	 * Append to a file.
	 *
	 * @param  string  $path
	 * @param  string  $data
	 * @return int
	 */
55
	public static function append($path, $data)
56 57
	{
		return file_put_contents($path, $data, LOCK_EX | FILE_APPEND);
58 59 60
	}

	/**
61 62 63 64 65
	 * Delete a file.
	 *
	 * @param  string  $path
	 * @return void
	 */
66
	public static function delete($path)
67
	{
68
		if (static::exists($path)) @unlink($path);
69 70 71
	}

	/**
Taylor Otwell committed
72
	 * Extract the file extension from a file path.
73
	 * 
Taylor Otwell committed
74
	 * @param  string  $path
75 76
	 * @return string
	 */
77
	public static function extension($path)
78 79 80 81
	{
		return pathinfo($path, PATHINFO_EXTENSION);
	}

82
	/**
83 84 85 86 87
	 * Get the file type of a given file.
	 *
	 * @param  string  $path
	 * @return string
	 */
88
	public static function type($path)
89 90 91 92 93 94 95
	{
		return filetype($path);
	}

	/**
	 * Get the file size of a given file.
	 *
Phill Sparks committed
96
	 * @param  string  $path
97 98
	 * @return int
	 */
99
	public static function size($path)
100 101 102 103 104 105 106 107 108 109
	{
		return filesize($path);
	}

	/**
	 * Get the file's last modification time.
	 *
	 * @param  string  $path
	 * @return int
	 */
110
	public static function modified($path)
111 112 113 114 115
	{
		return filemtime($path);
	}

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

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

136
		return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
137 138
	}

139
	/**
Taylor Otwell committed
140 141
	 * Determine if a file is a given type.
	 *
142
	 * The Fileinfo PHP extension is used to determine the file's MIME type.
Taylor Otwell committed
143
	 *
144 145 146 147 148 149 150 151
	 * <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>
	 *
Phill Sparks committed
152
	 * @param  array|string  $extensions
153
	 * @param  string        $path
Taylor Otwell committed
154
	 * @return bool
Taylor Otwell committed
155
	 */
156
	public static function is($extensions, $path)
Taylor Otwell committed
157
	{
158 159
		$mimes = Config::get('mimes');

160 161 162 163 164 165
		$mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);

		// The MIME configuration file contains an array of file extensions and
		// their associated MIME types. We will spin through each extension the
		// developer wants to check to determine if the file's MIME type is in
		// the list of MIMEs we have for that extension.
166
		foreach ((array) $extensions as $extension)
Taylor Otwell committed
167
		{
168 169 170 171
			if (isset($mimes[$extension]) and in_array($mime, (array) $mimes[$extension]))
			{
				return true;
			}
172
		}
Taylor Otwell committed
173

174
		return false;
Taylor Otwell committed
175 176
	}

177
	/**
178 179 180 181 182 183 184 185 186 187 188 189 190
	 * Move a directory from one location to another.
	 *
	 * @param  string  $source
	 * @param  string  $destination
	 * @param  int     $options
	 * @return void
	 */
	public static function mvdir($source, $destination, $options = fIterator::SKIP_DOTS)
	{
		static::cpdir($source, $destination, true, $options);
	}

	/**
191 192 193 194
	 * Recursively copy directory contents to another directory.
	 *
	 * @param  string  $source
	 * @param  string  $destination
195
	 * @param  bool    $delete
196
	 * @param  int     $options
197 198
	 * @return void
	 */
199
	public static function cpdir($source, $destination, $delete = false, $options = fIterator::SKIP_DOTS)
200 201 202 203 204 205 206 207
	{
		if ( ! is_dir($source)) return;

		// First we need to create the destination directory if it doesn't
		// already exists. This directory hosts all of the assets we copy
		// from the installed bundle's source directory.
		if ( ! is_dir($destination))
		{
208
			mkdir($destination, 0777, true);
209 210
		}

211
		$items = new fIterator($source, $options);
212 213 214 215 216 217 218 219 220 221 222 223 224

		foreach ($items as $item)
		{
			$location = $destination.DS.$item->getBasename();

			// If the file system item is a directory, we will recurse the
			// function, passing in the item directory. To get the proper
			// destination path, we'll add the basename of the source to
			// to the destination directory.
			if ($item->isDir())
			{
				$path = $item->getRealPath();

225
				static::cpdir($path, $location, $delete, $options);
226

227
				if ($delete) @rmdir($item->getRealPath());
228 229 230 231 232 233 234 235
			}
			// If the file system item is an actual file, we can copy the
			// file from the bundle asset directory to the public asset
			// directory. The "copy" method will overwrite any existing
			// files with the same name.
			else
			{
				copy($item->getRealPath(), $location);
236

237
				if ($delete) @unlink($item->getRealPath());
238 239
			}
		}
240 241

		if ($delete) rmdir($source);
242 243
	}

244
	/**
245
	 * Recursively delete a directory.
246
	 *
247
	 * @param  string  $directory
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	 * @return void
	 */
	public static function rmdir($directory)
	{
		if ( ! is_dir($directory)) return;

		$items = new fIterator($directory);

		foreach ($items as $item)
		{
			// If the item is a directory, we can just recurse into the
			// function and delete that sub-directory, otherwise we'll
			// just deleete the file and keep going!
			if ($item->isDir())
			{
				static::rmdir($item->getRealPath());
			}
			else
			{
				@unlink($item->getRealPath());
			}
		}

		@rmdir($directory);
	}

	/**
275 276 277
	 * Get the most recently modified file in a directory.
	 *
	 * @param  string       $directory
278
	 * @param  int          $options
279 280
	 * @return SplFileInfo
	 */
281
	public static function latest($directory, $options = fIterator::SKIP_DOTS)
282 283 284
	{
		$time = 0;

285
		$items = new fIterator($directory, $options);
286 287 288 289 290 291 292 293 294 295 296 297 298

		// To get the latest created file, we'll simply spin through the
		// directory, setting the latest file if we encounter a file
		// with a UNIX timestamp greater than the latest one we
		// have encountered thus far in the loop.
		foreach ($items as $item)
		{
			if ($item->getMTime() > $time) $latest = $item;
		}

		return $latest;
	}

299
}