mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-04-27 22:33:10 +09:00
Add streaming options to Storage::read() and Storage::write()
This commit is contained in:
parent
4fa3bf8cfb
commit
36bc83680f
2 changed files with 72 additions and 9 deletions
|
|
@ -148,17 +148,27 @@ class Storage
|
|||
* Get the content of a file.
|
||||
*
|
||||
* This method returns the content if it exists and is readable.
|
||||
* Otherwise, it returns false.
|
||||
* If $stream is true, it will return the content as a stream instead of
|
||||
* loading the entire content in memory. This may be useful for large files.
|
||||
* If the file cannot be opened, this method returns false.
|
||||
*
|
||||
* @param string $filename
|
||||
* @return string|false
|
||||
* @param bool $stream (optional)
|
||||
* @return string|resource|false
|
||||
*/
|
||||
public static function read($filename)
|
||||
public static function read($filename, $stream = false)
|
||||
{
|
||||
$filename = rtrim($filename, '/\\');
|
||||
if (self::exists($filename) && @is_file($filename) && @is_readable($filename))
|
||||
{
|
||||
return @file_get_contents($filename);
|
||||
if ($stream)
|
||||
{
|
||||
return @fopen($filename, 'r');
|
||||
}
|
||||
else
|
||||
{
|
||||
return @file_get_contents($filename);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -190,10 +200,12 @@ class Storage
|
|||
/**
|
||||
* Write $content to a file.
|
||||
*
|
||||
* If $content is a stream, this method will copy it to the target file
|
||||
* without loading the entire content in memory. This may be useful for large files.
|
||||
* This method returns true on success and false on failure.
|
||||
*
|
||||
* @param string $filename
|
||||
* @param string $content
|
||||
* @param string|resource $content
|
||||
* @param string $mode (optional)
|
||||
* @param int $perms (optional)
|
||||
* @return string|false
|
||||
|
|
@ -211,9 +223,26 @@ class Storage
|
|||
}
|
||||
}
|
||||
|
||||
$flags = ($mode === 'a') ? (\FILE_APPEND | \LOCK_EX) : (\LOCK_EX);
|
||||
$write_success = @file_put_contents($filename, $content, $flags);
|
||||
$result = ($write_success === strlen($content)) ? true : false;
|
||||
if ($fp = fopen($filename, $mode))
|
||||
{
|
||||
flock($fp, \LOCK_EX);
|
||||
if (is_resource($content))
|
||||
{
|
||||
$result = stream_copy_to_stream($content, $fp) ? true : false;
|
||||
}
|
||||
else
|
||||
{
|
||||
$result = fwrite($fp, $content) ? true : false;
|
||||
}
|
||||
fflush($fp);
|
||||
flock($fp, \LOCK_UN);
|
||||
fclose($fp);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@chmod($filename, ($perms === null ? (0666 & ~umask()) : $perms));
|
||||
if (function_exists('opcache_invalidate') && substr($filename, -4) === '.php')
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue