Files
lgp-admin-plus-api/laravel/file.php

181 lines
3.3 KiB
PHP
Raw Normal View History

2011-08-18 19:56:29 -05:00
<?php namespace Laravel;
class File {
2011-06-17 00:17:51 -05:00
/**
* Determine if a file exists.
*
* @param string $path
* @return bool
*/
public static function exists($path)
{
return file_exists($path);
}
/**
* Get the contents of a file.
*
* @param string $path
* @return string
*/
public static function get($path)
{
return file_get_contents($path);
}
/**
* 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);
}
/**
* 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);
}
/**
* Delete a file.
*
* @param string $path
* @return void
*/
public static function delete($path)
{
if (static::exists($path)) @unlink($path);
}
/**
2011-08-22 22:25:53 -05:00
* Extract the file extension from a file path.
*
2011-06-17 00:17:51 -05:00
* @param string $path
* @return string
*/
public static function extension($path)
{
return pathinfo($path, PATHINFO_EXTENSION);
}
/**
* Get the file type of a given file.
*
* @param string $path
* @return string
*/
public static function type($path)
{
return filetype($path);
}
/**
* Get the file size of a given file.
*
* @param string $file
* @return int
*/
public static function size($path)
{
return filesize($path);
}
/**
* Get the file's last modification time.
*
* @param string $path
* @return int
*/
public static function modified($path)
{
return filemtime($path);
}
2011-09-08 17:49:16 -05:00
/**
* Move an uploaded file to permanenet storage.
*
* @param string $key
* @param string $path
* @param array $files
* @return bool
*/
public static function upload($key, $path, $files)
2011-09-08 17:49:16 -05:00
{
return move_uploaded_file($files[$key]['tmp_name'], $path);
}
/**
* Get a file MIME type by extension.
*
* @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];
}
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
*
2011-09-08 17:49:16 -05:00
* @param array|string $extension
* @param string $path
2011-08-22 22:30:58 -05:00
* @return bool
2011-06-26 12:28:48 -05:00
*/
public static function is($extensions, $path)
2011-06-26 12:28:48 -05:00
{
$mimes = Config::get('mimes');
2011-09-08 17:49:16 -05:00
foreach ((array) $extensions as $extension)
2011-06-26 12:28:48 -05:00
{
2011-09-08 17:49:16 -05:00
$mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
2011-06-26 12:28:48 -05:00
if (isset($mimes[$extension]) and in_array((array) $mimes[$extension])) return true;
2011-09-08 17:49:16 -05:00
}
2011-06-26 12:28:48 -05:00
2011-09-08 17:49:16 -05:00
return false;
2011-06-26 12:28:48 -05:00
}
2011-09-21 21:46:16 -05:00
/**
* Get the lines surrounding a given line in a file.
*
* @param string $path
* @param int $line
* @param int $padding
* @return array
*/
public static function snapshot($path, $line, $padding = 5)
{
if ( ! file_exists($path)) return array();
$file = file($path, FILE_IGNORE_NEW_LINES);
array_unshift($file, '');
if (($start = $line - $padding) < 0) $start = 0;
if (($length = ($line - $start) + $padding + 1) < 0) $length = 0;
return array_slice($file, $start, $length, true);
}
}