★ wanayoo — archive 1999 http://www.php.net/manual/ja/function.fgets.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to ファイルシステム
Quick Reference
English version of this pageGerman version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
ファイルシステム
* basename
* chgrp
* chmod
* chown
* clearstatcache
* copy
* delete
* dirname
* diskfreespace
* fclose
* feof
* fgetc
* fgetcsv
* fgets
* fgetss
* file
* file_exists
* fileatime
* filectime
* filegroup
* fileinode
* filemtime
* fileowner
* fileperms
* filesize
* filetype
* flock
* fopen
* fpassthru
* fputs
* fread
* fscanf
* fseek
* fstat
* ftell
* ftruncate
* fwrite
* set_file_buffer
* is_dir
* is_executable
* is_file
* is_link
* is_readable
* is_writeable
* link
* linkinfo
* mkdir
* pclose
* popen
* readfile
* readlink
* rename
* rewind
* rmdir
* stat
* lstat
* realpath
* symlink
* tempnam
* touch
* umask
* unlink
Manual: fgets
View the source code for this pageSearch the site



Previous page
 fgetcsv
 Updated
Sat, 12 Aug 2000
fgetss 
Next page


fgets

(PHP3 , PHP4 )

fgets -- ファイルポインタから1行取得する

説明

string fgets (int fp, int length)

fpで指定したファイルポインタから最大length-1バイト読み出し、 その文字列を返します。読み出しは、length-1バイト読み出したか、 (返り値に含まれる)改行文字を検出したか、 EOFに達したかのいずれかが起こった時点で終了します。

エラーが起こった場合、falseを返します。

陥りやすい罠:

C言語のfgetsの動作に慣れている人は、EOFを返す条件の違いについて 注意する必要があります。

ファイルポインタは有効なものでなければならず、また fopen()popen()fsockopen()で正常にオープンされたファイルを 指している必要があります。

簡単な例を以下に示します。

例 1. 行毎にファイルを読み込む


$fd = fopen ("/tmp/inputfile.txt", "r");
while (!feof($fd)) {
  $buffer = fgets($fd, 4096);
  echo $buffer;
}
fclose ($fd);
      

fread(), fopen(), popen(), fgetc(), fsockopen()も参照下さい。


User Contributed Notes: fgets


patrickc@espider.com
13-Jan-2000 11:09
i don't know how to handle the array string:
The result of username and the string length not right~
e.g.
username : "abc "
strlen : 5
e.g. (username.txt)
abc
cde
efgh

Program :
if($myFile = fopen("username.txt","r")){
while(!feof($myFile)){
$username[]=fgets($myFile,20);
}
// Count total user number
$totaluser = count($username);
fclose($myFile);
}

Please help me~~thanks~



hellekin@imaginet.fr
22-Mar-2000 01:10
<pre>You should use :

$users = file("username.txt");
$number_of_users = sizeof($users);

fgets() returns one line as a string
while
file() returns all lines as an array.
</pre>



info@dsdi.de
28-Mar-2000 06:15
For some hosts ($mxhost) the fgets never stops, also maximum execution time is never reached:

<PRE>$fp = fsockopen ( $mxhost, 25, $errno, &$errstr, 30 );
fputs ( $fp, "HELO $SERVER_NAME\n" );
$result = fgets ( $fp, 2000 );</PRE>

Enviroment: Linux Kernel 2.2.5, php 3.0.12



yung3890@kimo.com.tw
30-Mar-2000 03:24
I have a text file ,like this :
0012345 432 12345 567
0023456 656456 234
.....
the line length is fixed .
now I use $str=fgets($fp,"text file") to read this file
But when I echo $str ,the browser can't
show the white spaces.
it looks like :
0012345 432 12345 567
0023456 656456 234
....
I hope the browser can show the white spaces.





c_sidcar@club-internet.fr
16-May-2000 08:26
fgets return a line terminated by \n in Unix
and \r\n in Windows.
It's hard to test php script on Windows before using it in Unix !!



froggywwu@hotmail.com
29-Jun-2000 06:57
I was using fgets to get one byte at a time from a file and I was using 1 for the length, e.g. fgets($fp,1). This should give you one character from the file, so I put it in a do while loop which terminates when it finds a space. Using length one does not grab one byte from the file it grabs maybe a space or something, what you need to do to grab a singe byte from a file you need to have length 2, otherwise you will cause the webserver to crash or become extrememly slow.


 About Notes


Previous page
 fgetcsv
 Updated
Sat, 12 Aug 2000
fgetss 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.