Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F92592129
LinesOfALargeFile.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Nov 21, 19:16
Size
2 KB
Mime Type
text/x-php
Expires
Sat, Nov 23, 19:16 (2 d)
Engine
blob
Format
Raw Data
Handle
22466041
Attached To
rPHU libphutil
LinesOfALargeFile.php
View Options
<?php
/**
* Read the lines of a file, one at a time. This allows you to process large
* files without holding them in memory. In most cases, it is more efficient
* (and certainly simpler) to read the entire file and `explode()` it. For more
* information, see @{class:LinesOfALarge}. See also
* @{class:LinesOfALargeExecFuture}, for a similar abstraction that works on
* executed commands.
*
* foreach (new LinesOfALargeFile('/some/large/logfile.log') as $line) {
* // ...
* }
*
* If the file can not be read, a @{class:FilesystemException} is thrown.
*
* @task construct Construction
* @task internals Internals
* @group filesystem
*/
final
class
LinesOfALargeFile
extends
LinesOfALarge
{
private
$fileName
;
private
$handle
;
/* -( Construction )------------------------------------------------------- */
/**
* To construct, pass the path to a file.
*
* @param string File path to read.
* @return void
* @task construct
*/
public
function
__construct
(
$file_name
)
{
$this
->
fileName
=
Filesystem
::
resolvePath
((
string
)
$file_name
);
}
/* -( Internals )---------------------------------------------------------- */
/**
* Closes the file handle.
*
* @return void
* @task internals
*/
public
function
__destruct
()
{
$this
->
closeHandle
();
}
/**
* Close the file handle, if it is open.
*
* @return $this
* @task internals
*/
private
function
closeHandle
()
{
if
(
$this
->
handle
)
{
fclose
(
$this
->
handle
);
$this
->
handle
=
null
;
}
return
$this
;
}
/**
* Closes the file handle if it is open, and reopens it.
*
* @return void
* @task internals
*/
protected
function
willRewind
()
{
$this
->
closeHandle
();
$this
->
handle
=
@
fopen
(
$this
->
fileName
,
'r'
);
if
(!
$this
->
handle
)
{
throw
new
FilesystemException
(
$this
->
fileName
,
"Failed to open file!"
);
}
}
/**
* Read the file chunk-by-chunk.
*
* @return string Next chunk of the file.
* @task internals
*/
public
function
readMore
()
{
// NOTE: At least on OSX in reasonably normal test cases, increasing the
// size of this read has no impact on performance.
$more
=
@
fread
(
$this
->
handle
,
2048
);
if
(
$more
===
false
)
{
throw
new
FilesystemException
(
$this
->
fileName
,
"Failed to read file!"
);
}
return
$more
;
}
}
Event Timeline
Log In to Comment