The file pointer that fgets() uses can also be created with the proc_open() function and used with the stdout pipe created from the executed process.
fgets
(PHP 4, PHP 5)
fgets — ファイルポインタから 1 行取得する
説明
ファイルポインタから 1 行取得します。
パラメータ
- handle
-
ファイルポインタは、有効なファイルポインタである必要があり、 fopen() または fsockopen() で正常にオープンされた (そしてまだ fclose() でクローズされていない) ファイルを指している必要があります。
- length
-
読み出しは、length - 1 バイト読み出したか、(返り値に含まれる) 改行文字を検出したか、EOF に達したかのいずれかが起こった時点で終了します。 length が指定されない場合は、行末に達するまで読み続けます。
注意: PHP 4.3.0 より前のバージョンでは、もしこのパラメータが省略された場合、 行の長さを 1024 と仮定していました。 もしもファイル内の行の多くが 8KB を超えている場合、 行の長さの最大値を特定するためにスクリプトはリソースの影響を より大きく受けることになります。
返り値
handle で指定したファイルポインタから最大 length - 1 バイト読み出し、 その文字列を返します。
エラーが起こった場合、FALSE を返します。
変更履歴
| バージョン | 説明 |
|---|---|
| 4.3.0 | fgets() はバイナリセーフとなりました。 |
| 4.2.0 | length パラメータがオプションとなりました。 |
例
Example#1 行毎にファイルを読み込む
<?php
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
echo $buffer;
}
fclose($handle);
}
?>
注意
注意: マッキントッシュコンピュータ上で作成されたファイルを読み込む際に、 PHP が行末を認識できないという問題が発生した場合、 実行時の設定オプションauto_detect_line_endings を有効にする必要が生じるかもしれません。
注意: C 言語の fgets() の動作に慣れている人は、 EOF を返す条件の違いについて注意する必要があります。
fgets
26-Jan-2008 01:47
25-Oct-2007 09:32
If you use the example from the command-description, i recommend to trim the $buffer for further use. The line feed ist still at the end of the line. I saw this when using PHP CLI.
Like this, checking a file-list for existing entries:
$handle = fopen ("/tmp/files.txt", "r");
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
if (file_exists(rtrim($filename,"\n"))) {
echo $buffer;
} else {
echo $buffer." has been removed."
}
fclose ($handle);
19-Sep-2007 06:15
I'm using this function to modify the header of a large postscript document on copy... Works extremely quickly so far...
function write($filename) {
$fh = fopen($this->sourceps,'r');
$fw = fopen($filename,'w');
while (!feof($fh)) {
$buffer = fgets($fh);
fwrite($fw,$buffer);
if (!$setupfound && ereg("^%%BeginSetup",$buffer)) {
$setupfound++;
if (array_key_exists("$filename",$this->output)) {
foreach ($this->output[$filename] as $function => $value) {
fwrite($fw,$value);
}
}
stream_copy_to_stream($fh,$fw);
}
}
fclose($fw);
fclose($fh);
}
22-Aug-2007 03:36
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)
11-Jul-2007 06:23
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".
22-Jun-2007 08:12
I would like to know if there is a way to pull out information from a ,txt file so that it will show only a section of the file in the browser and no the whole file. these are small files of 10 lines but need to be able to pull specific information in the txt file out into the page.
I am new to php and functions and I know how to pull the file into the page but can not seem to get the page to show a character range etc of the information from the file
13-Aug-2006 05:03
For sockets, If you dont want fgets, fgetc etc... to block if theres no data there. set socket_set_blocking(handle,false); and socket_set_blocking(handle,true); to set it back again.
15-Jul-2006 06:21
fgets is SLOW for scanning through large files. If you don't have PHP 5, use fscanf($file, "%s\n") instead.
23-May-2006 06:09
An easy way to authenticate Windows Domain users from scripts running on a non-Windows or non-Domain box - pass the submitted username and password to an IMAP service on a Windows machine.
<?php
$server = 'imapserver';
$user = 'user';
$pass = 'pass';
if (authIMAP($user, $pass, $server)) {
echo "yay";
} else {
echo "nay";
}
function authIMAP($user, $pass, $server) {
$connection = fsockopen($server, 143, $errno, $errstr, 30);
if(!$connection) return false;
$output = fgets($connection, 128); // banner
fputs($connection, "1 login $user $pass\r\n");
$output = fgets($connection, 128);
fputs($connection, "2 logout\r\n");
fclose($connection);
if (substr($output, 0, 4) == '1 OK') return true;
return false;
}
?>
Macintosh line endings mentioned in docs refer to Mac OS Classic. You don't need this setting for interoperability with unixish OS X.
09-Mar-2006 08:44
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
05-Jan-2006 06:20
I would have expected the same behaviour from these bits of code:-
<?php
/*This times out correctly*/
while (!feof($fp)) {
echo fgets($fp);
}
/*This times out before eof*/
while ($line=fgets($fp)) {
echo $line;
}
/*A reasonable fix is to set a long timeout*/
stream_set_timeout($fp, 180);
while ($line=fgets($fp)) {
echo $line;
}
?>
06-Dec-2005 05:17
When working with VERY large files, php tends to fall over sideways and die.
Here is a neat way to pull chunks out of a file very fast and won't stop in mid line, but rater at end of last known line. It pulled a 30+ million line 900meg file through in ~ 24 seconds.
NOTE:
$buf just hold current chunk of data to work with. If you try "$buf .=" (note 'dot' in from of '=') to append $buff, script will come to grinding crawl around 100megs of data, so work with current data then move on!
//File to be opened
$file = "huge.file";
//Open file (DON'T USE a+ pointer will be wrong!)
$fp = fopen($file, 'r');
//Read 16meg chunks
$read = 16777216;
//\n Marker
$part = 0;
while(!feof($fp)) {
$rbuf = fread($fp, $read);
for($i=$read;$i > 0 || $n == chr(10);$i--) {
$n=substr($rbuf, $i, 1);
if($n == chr(10))break;
//If we are at the end of the file, just grab the rest and stop loop
elseif(feof($fp)) {
$i = $read;
$buf = substr($rbuf, 0, $i+1);
break;
}
}
//This is the buffer we want to do stuff with, maybe thow to a function?
$buf = substr($rbuf, 0, $i+1);
//Point marker back to last \n point
$part = ftell($fp)-($read-($i+1));
fseek($fp, $part);
}
fclose($fp);
01-Dec-2005 10:51
It appears that fgets() will return FALSE on EOF (before feof has a chance to read it), so this code will throw an exception:
while (!feof($fh)) {
$line = fgets($fh);
if ($line === false) {
throw new Exception("File read error");
}
}
08-Jan-2005 04:11
Saku's example may also be used like this:
<?php
@ $pointer = fopen("$DOCUMENT_ROOT/foo.txt", "r"); // the @ suppresses errors so you have to test the pointer for existence
if ($pointer) {
while (!feof($pointer)) {
$preTEXT = fgets($pointer, 999);
// $TEXT .= $preTEXT; this is better for a string
$ATEXT[$I] = $preTEXT; // maybe better as an array
$I++;
}
fclose($pointer);
}
?>
19-Nov-2004 11:43
Sometimes the strings you want to read from a file are not separated by an end of line character. the C style getline() function solves this. Here is my version:
<?php
function getline( $fp, $delim )
{
$result = "";
while( !feof( $fp ) )
{
$tmp = fgetc( $fp );
if( $tmp == $delim )
return $result;
$result .= $tmp;
}
return $result;
}
// Example:
$fp = fopen("/path/to/file.ext", 'r');
while( !feof($fp) )
{
$str = getline($fp, '|');
// Do something with $str
}
fclose($fp);
?>
04-Nov-2004 07:54
Note that - afaik - fgets reads a line until it reaches a line feed (\\n). Carriage returns (\\r) aren't processed as line endings.
However, nl2br insterts a <br /> tag before carriage returns as well.
This is useful (but not nice - I must admit) when you want to store a more lines in one.
<?php
function write_lines($text) {
$file = fopen('data.txt', 'a');
fwrite($file, str_replace("\n", ' ', $text)."\n");
fclose($file);
}
function read_all() {
$file = fopen('data.txt', 'r');
while (!feof($file)) {
$line = fgets($file);
echo '<u>Section</u><p>nl2br'.($line).'</p>';
}
fclose($file);
}
?>
Try it.
If you need to simulate an un-buffered fgets so that stdin doesnt hang there waiting for some input (i.e. it reads only if there is data available) use this :
<?php
function fgets_u($pStdn) {
$pArr = array($pStdn);
if (false === ($num_changed_streams = stream_select($pArr, $write = NULL, $except = NULL, 0))) {
print("\$ 001 Socket Error : UNABLE TO WATCH STDIN.\n");
return FALSE;
} elseif ($num_changed_streams > 0) {
return trim(fgets($pStdn, 1024));
}
}
?>
13-Aug-2004 01:03
Take note that fgets() reads 'whole lines'. This means that if a file pointer is in the middle of the line (eg. after fscanf()), fgets() will read the following line, not the remaining part of the currnet line. You could expect it would read until the end of the current line, but it doesn't. It skips to the next full line.
17-Jun-2004 11:13
If you need to read an entire file into a string, use file_get_contents(). fgets() is most useful when you need to process the lines of a file separately.
05-Jun-2004 09:47
As a beginner I would have liked to see "how to read a file into a string for use later and not only how to directly echo the fgets() result. This is what I derived:
<?php
@ $pointer = fopen("$DOCUMENT_ROOT/foo.txt", "r"); // the @ suppresses errors so you have to test the pointer for existence
if ($pointer) {
while (!feof($pointer)) {
$preTEXT = fgets($pointer, 999);
$TEXT = $TEXT . $preTEXT;
}
fclose($pointer);
}
?>
23-Feb-2004 09:35
If you have troubles reading binary data with versions <= 4.3.2 then upgrade to 4.3.3
The binary safe implementation seems to have had bugs which were fixed in 4.3.3