fgets
(PHP 4, PHP 5)
fgets — Gets line from file pointer
Description
$handle
[, int $length
] )Gets a line from file pointer.
Parameters
-
handle
-
The file pointer must be valid, and must point to a file successfully opened by fopen() or fsockopen() (and not yet closed by fclose()).
-
length
-
Reading ends when
length
- 1 bytes have been read, or a newline (which is included in the return value), or an EOF (whichever comes first). If no length is specified, it will keep reading from the stream until it reaches the end of the line.Note:
Until PHP 4.3.0, omitting it would assume 1024 as the line length. If the majority of the lines in the file are all larger than 8KB, it is more resource efficient for your script to specify the maximum line length.
Return Values
Returns a string of up to length
- 1 bytes read from
the file pointed to by handle
. If there is no more data
to read in the file pointer, then FALSE
is returned.
If an error occurs, FALSE
is returned.
Changelog
Version | Description |
---|---|
4.3.0 | fgets() is now binary safe |
4.2.0 |
The length parameter became optional
|
Examples
Example #1 Reading a file line by line
<?php
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
echo $buffer;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
?>
Notes
Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem.
Note:
People used to the 'C' semantics of fgets() should note the difference in how EOF is returned.
See Also
- fgetss() - Gets line from file pointer and strip HTML tags
- fread() - Binary-safe file read
- fgetc() - Gets character from file pointer
- stream_get_line() - Gets line from stream resource up to a given delimiter
- fopen() - Opens file or URL
- popen() - Opens process file pointer
- fsockopen() - Open Internet or Unix domain socket connection
- stream_set_timeout() - Set timeout period on a stream
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с файловой системой
- Функции для работы с файловой системой
- basename
- chgrp
- chmod
- chown
- clearstatcache
- copy
- delete
- dirname
- disk_free_space
- disk_total_space
- diskfreespace
- fclose
- feof
- fflush
- fgetc
- fgetcsv
- fgets
- fgetss
- file_exists
- file_get_contents
- file_put_contents
- file
- fileatime
- filectime
- filegroup
- fileinode
- filemtime
- fileowner
- fileperms
- filesize
- filetype
- flock
- fnmatch
- fopen
- fpassthru
- fputcsv
- fputs
- fread
- fscanf
- fseek
- fstat
- ftell
- ftruncate
- fwrite
- glob
- is_dir
- is_executable
- is_file
- is_link
- is_readable
- is_uploaded_file
- is_writable
- is_writeable
- lchgrp
- lchown
- link
- linkinfo
- lstat
- mkdir
- move_uploaded_file
- parse_ini_file
- parse_ini_string
- pathinfo
- pclose
- popen
- readfile
- readlink
- realpath_cache_get
- realpath_cache_size
- realpath
- rename
- rewind
- rmdir
- set_file_buffer
- stat
- symlink
- tempnam
- tmpfile
- touch
- umask
- unlink
Коментарии
I think that the quickest way of read a (long) file with the rows in reverse order is
<?php
$myfile = 'myfile.txt';
$command = "tac $myfile > /tmp/myfilereversed.txt";
passthru($command);
$ic = 0;
$ic_max = 100; // stops after this number of rows
$handle = fopen("/tmp/myfilereversed.txt", "r");
while (!feof($handle) && ++$ic<=$ic_max) {
$buffer = fgets($handle, 4096);
echo $buffer."<br>";
}
fclose($handle);
?>
It echos the rows while it is reading the file so it is good for long files like logs.
Borgonovo
There's an error in the documentation:
The file pointer must be valid, and must point to a file successfully opened by fopen() or fsockopen() (and not yet closed by fclose()).
You should also add "popen" and "pclose" to the documentation. I'm a new PHP developer and went to verify that I could use "fgets" on commands that I used with "popen".
fscanf($file, "%s\n") isn't really a good substitution for fgets(), since it will stop parsing at the first whitespace and not at the end of line!
(See the fscanf page for details on this)
A better example, to illustrate the differences in speed for large files, between fgets and stream_get_line.
This example simulates situations where you are reading potentially very long lines, of an uncertain length (but with a maximum buffer size), from an input source.
As Dade pointed out, the previous example I provided was much to easy to pick apart, and did not adequately highlight the issue I was trying to address.
Note that specifying a definitive end-character for fgets (ie: newline), generally decreases the speed difference reasonably significantly.
#!/usr/bin/php
<?php
$plaintext=file_get_contents('http://loripsum.net/api/60/verylong/plaintext'); # Should be around 90k characters
$plaintext=str_replace("\n"," ",$plaintext); # Get rid of newlines
$fp=fopen("/tmp/SourceFile.txt","w");
for($i=0;$i<100000;$i++) {
fputs($fp,substr($plaintext,0,rand(4096,65534)) . "\n");
}
fclose($fp);
$fp=fopen("/tmp/SourceFile.txt","r");
$start=microtime(true);
while($line=fgets($fp,65535)) {
1;
}
$end=microtime(true);
fclose($fp);
$delta1=($end - $start);
$fp=fopen("/tmp/SourceFile.txt","r");
$start=microtime(true);
while($line=stream_get_line($fp,65535)) {
1;
}
$end=microtime(true);
fclose($fp);
$delta2=($end - $start);
$pdiff=$delta1/$delta2;
print "stream_get_line is " . ($pdiff>1?"faster":"slower") . " than fgets - pdiff is $pdiff\n";
?>
$ ./testcase.php
stream_get_line is faster than fgets - pdiff is 1.760398041785
Note that, in a vast majority of situations in which php is employed, tiny differences in speed between system calls are of negligible importance.
if you for some reason need to get lines from a string instead of a file pointer, try
<?php
function string_gets(string $source, int $offset = 0, string $delimiter = "\n"): ?string
{
$len = strlen($source);
if ($len < $offset) {
// out of bounds.. maybe i should throw an exception
return null;
}
if ($len === $offset) {
// end of string..
return null;
}
$delimiter_pos = strpos($source, $delimiter, $offset);
if ($delimiter_pos === false) {
// last line.
return substr($source, $offset);
}
return substr($source, $offset, ($delimiter_pos - $offset) + strlen($delimiter));
}
?>
(i had a ~16GB string in-memory i needed to process line-by-line, but i would get memory-allocation-crash (on a 32GB ram system) if i tried explode("\n",$str); , so came up with this.. interestingly, fgets() seems to be faster than doing it in-ram-in-php, though. php 7.3.7)