fgets() - 从文件指针中读取一行 - php 文件目录函数
fgets()
(PHP 4, PHP 5, PHP 7)
从文件指针中读取一行
说明
fgets(resource $handle[,int $length]): string从文件指针中读取一行。
参数
$handle文件指针必须是有效的,必须指向由fopen()或fsockopen()成功打开的文件(并还未由fclose()关闭)。
$length从$handle指向的文件中读取一行并返回长度最多为$length- 1 字节的字符串。碰到换行符(包括在返回值中)、EOF 或者已经读取了 length - 1 字节后停止(看先碰到那一种情况)。如果没有指定$length,则默认为 1K,或者说 1024 字节。
Note:从 PHP 4.3 开始,忽略掉 length 则行的长度被假定为 1024,将继续从流中读取数据直到行结束。如果文件中的大多数行都大于 8KB,则在脚本中指定最大行的长度在利用资源上更为有效。
返回值
从指针$handle指向的文件中读取了$length- 1 字节后返回字符串。如果文件指针中没有更多的数据了则返回FALSE
。
错误发生时返回FALSE
。
更新日志
版本 | 说明 |
---|---|
4.3.0 | fgets()开始二进制安全 |
4.2.0 | $length参数成为可选。 |
范例
逐行读取文件
注释
Note:在读取在 Macintosh 电脑中或由其创建的文件时,如果 PHP不能正确的识别行结束符,启用运行时配置可选项auto_detect_line_endings也许可以解决此问题。Note:
习惯了 C 语言中fgets()语法的人应该注意到EOF是怎样被返回的。
参见
fgetss()
从文件指针中读取一行并过滤掉 HTML 标记fread()
读取文件(可安全用于二进制文件)fgetc()
从文件指针中读取字符stream_get_line()
从资源流里读取一行直到给定的定界符fopen()
打开文件或者 URLpopen()
打开进程文件指针fsockopen()
打开一个网络连接或者一个Unix套接字连接stream_set_timeout()
Set timeout period on a stream
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 $ ./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.
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".
One thing I discovered with fgets, at least with PHP 5.1.6, is that you may have to use an IF statement to avoid your code running rampant (and possibly hanging the server). This can cause problems if you do not have root access on the server on which you are working. This is the code I have implemented ($F1 is an array): I have noticed that without the IF statement, fgets seems to ignore when $fh is undefined (i.e., "filename" does not exist). If that happens, it will keep attempting to read from a nonexistent filehandle until the process can be administratively killed or the server hangs, whichever comes first.
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.
I had loads of trouble while reading with fgets on a WAMP (Windows server). On local the file went unto atag without a hitch, but when I moved the code to a LAMP production server, every \r\n created two fgets and I got free empty lines. I tried deleting with $string=str_replace("\r\n","\n",$string); but it had no effect whatsoever. The solution was to do an fread() and explode the contents by PHP_EOL and do a foreach($lines as $line) so every line did not get duplicated. Here is the example code: $file=fopen("test.txt,"r"); $text=fread($file,filesize("test.txt")); $lines=explode(PHP_EOL,$text); foreach($lines as $line) { // Do something }Macintosh line endings mentioned in docs refer to Mac OS Classic. You don't need this setting for interoperability with unixish OS X.I think that the quickest way of read a (long) file with the rows in reverse order is It echos the rows while it is reading the file so it is good for long files like logs. BorgonovoIt's strange no one mentions "0" in this context. Since "0" is considered to be false, a line with a single "0" can be treated as EOF if using the while assign idiom. while ($line = fgets(STDIN, 2)) { } This may surprisingly break if a line starts with ")"
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)