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

PHP Home Page

Manual Table of Contents
Up to Filesystem
Quick Reference
English version of this pageGerman version of this pageJapanese version of this pageFrench version of this pageHungarian version of this page
Filesystem
* 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
* fseek
* 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
* symlink
* tempnam
* touch
* umask
* unlink
Manual: fgets
View the source code for this pageSearch the site



Previous page
 fgetcsv
 Updated
Sun, 06 Aug 2000
fgetss 
Next page


fgets

(PHP3 , PHP4 )

fgets -- Get line from file pointer

Description

string fgets (int fp, int length)

Returns a string of up to length - 1 bytes read from the file pointed to by fp. Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first).

If an error occurs, returns false.

Common Pitfalls:

People used to the 'C' semantics of fgets should note the difference in how EOF is returned.

The file pointer must be valid, and must point to a file successfully opened by fopen(), popen(), or fsockopen().

A simple example follows:

Esempio 1. Reading a file line by line


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

See also fread(), fopen(), popen(), fgetc(), and 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
Sun, 06 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.