2011-08-18 19:56:29 -05:00
|
|
|
<?php namespace Laravel;
|
2011-06-16 23:51:28 -05:00
|
|
|
|
|
|
|
|
class File {
|
2011-06-17 00:17:51 -05:00
|
|
|
|
2011-08-26 21:42:04 -05:00
|
|
|
/**
|
|
|
|
|
* All of the MIME types understood by the manager.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
private $mimes;
|
|
|
|
|
|
|
|
|
|
/**
|
2011-08-30 22:35:32 -05:00
|
|
|
* Create a new file engine instance.
|
2011-08-26 21:42:04 -05:00
|
|
|
*
|
|
|
|
|
* @param array $mimes
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct($mimes)
|
|
|
|
|
{
|
|
|
|
|
$this->mimes = $mimes;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-25 22:53:05 -05:00
|
|
|
/**
|
|
|
|
|
* Determine if a file exists.
|
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function exists($path)
|
|
|
|
|
{
|
|
|
|
|
return file_exists($path);
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-17 00:42:23 -05:00
|
|
|
/**
|
2011-06-17 12:23:26 -05:00
|
|
|
* Get the contents of a file.
|
2011-06-17 00:42:23 -05:00
|
|
|
*
|
|
|
|
|
* @param string $path
|
2011-06-17 12:23:26 -05:00
|
|
|
* @return string
|
2011-06-17 00:42:23 -05:00
|
|
|
*/
|
2011-08-25 22:53:05 -05:00
|
|
|
public function get($path)
|
2011-06-17 00:42:23 -05:00
|
|
|
{
|
2011-06-17 12:23:26 -05:00
|
|
|
return file_get_contents($path);
|
|
|
|
|
}
|
2011-06-17 00:42:23 -05:00
|
|
|
|
2011-06-17 12:23:26 -05:00
|
|
|
/**
|
|
|
|
|
* Write to a file.
|
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param string $data
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2011-08-25 22:53:05 -05:00
|
|
|
public function put($path, $data)
|
2011-06-17 12:23:26 -05:00
|
|
|
{
|
|
|
|
|
return file_put_contents($path, $data, LOCK_EX);
|
|
|
|
|
}
|
2011-06-17 00:42:23 -05:00
|
|
|
|
2011-06-17 12:23:26 -05:00
|
|
|
/**
|
|
|
|
|
* Append to a file.
|
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param string $data
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2011-08-25 22:53:05 -05:00
|
|
|
public function append($path, $data)
|
2011-06-17 12:23:26 -05:00
|
|
|
{
|
|
|
|
|
return file_put_contents($path, $data, LOCK_EX | FILE_APPEND);
|
2011-06-17 00:42:23 -05:00
|
|
|
}
|
|
|
|
|
|
2011-08-25 22:53:05 -05:00
|
|
|
/**
|
|
|
|
|
* Delete a file.
|
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function delete($path)
|
|
|
|
|
{
|
|
|
|
|
@unlink($path);
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-16 23:51:28 -05:00
|
|
|
/**
|
2011-08-22 22:25:53 -05:00
|
|
|
* Extract the file extension from a file path.
|
2011-06-16 23:51:28 -05:00
|
|
|
*
|
2011-06-17 00:17:51 -05:00
|
|
|
* @param string $path
|
2011-06-16 23:51:28 -05:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-08-25 22:53:05 -05:00
|
|
|
public function extension($path)
|
2011-06-16 23:51:28 -05:00
|
|
|
{
|
|
|
|
|
return pathinfo($path, PATHINFO_EXTENSION);
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-25 22:53:05 -05:00
|
|
|
/**
|
|
|
|
|
* Get the file type of a given file.
|
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function type($path)
|
|
|
|
|
{
|
|
|
|
|
return filetype($path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the file size of a given file.
|
|
|
|
|
*
|
|
|
|
|
* @param string $file
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function size($path)
|
|
|
|
|
{
|
|
|
|
|
return filesize($path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the file's last modification time.
|
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function modified($path)
|
|
|
|
|
{
|
|
|
|
|
return filemtime($path);
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-17 00:42:23 -05:00
|
|
|
/**
|
2011-06-17 12:23:26 -05:00
|
|
|
* Get a file MIME type by extension.
|
2011-06-17 00:42:23 -05:00
|
|
|
*
|
|
|
|
|
* @param string $extension
|
|
|
|
|
* @param string $default
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-08-25 22:53:05 -05:00
|
|
|
public function mime($extension, $default = 'application/octet-stream')
|
2011-06-17 00:42:23 -05:00
|
|
|
{
|
2011-08-26 21:42:04 -05:00
|
|
|
if ( ! array_key_exists($extension, $this->mimes)) return $default;
|
2011-06-26 11:59:51 -05:00
|
|
|
|
2011-08-26 21:42:04 -05:00
|
|
|
return (is_array($this->mimes[$extension])) ? $this->mimes[$extension][0] : $this->mimes[$extension];
|
2011-06-17 00:42:23 -05:00
|
|
|
}
|
|
|
|
|
|
2011-06-26 12:28:48 -05:00
|
|
|
/**
|
|
|
|
|
* Determine if a file is a given type.
|
|
|
|
|
*
|
2011-08-29 22:02:32 -05:00
|
|
|
* The Fileinfo PHP extension will be used to determine the MIME type of the file.
|
2011-08-22 22:30:58 -05:00
|
|
|
*
|
|
|
|
|
* @param string $extension
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @return bool
|
2011-06-26 12:28:48 -05:00
|
|
|
*/
|
2011-08-25 22:53:05 -05:00
|
|
|
public function is($extension, $path)
|
2011-06-26 12:28:48 -05:00
|
|
|
{
|
2011-08-26 21:42:04 -05:00
|
|
|
if ( ! array_key_exists($extension, $this->mimes))
|
2011-06-26 12:28:48 -05:00
|
|
|
{
|
|
|
|
|
throw new \Exception("File extension [$extension] is unknown. Cannot determine file type.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
|
|
|
|
|
|
2011-08-26 21:42:04 -05:00
|
|
|
return (is_array($this->mimes[$extension])) ? in_array($mime, $this->mimes[$extension]) : $mime === $this->mimes[$extension];
|
2011-06-26 12:28:48 -05:00
|
|
|
}
|
|
|
|
|
|
2011-06-16 23:51:28 -05:00
|
|
|
}
|