PHP读取文件

本文概述

PHP提供了各种功能来从文件读取数据。有多种功能允许你读取所有文件数据, 逐行读取数据以及逐字符读取数据。

下面提供了可用的PHP文件读取功能。

  • fread()
  • fgets()
  • fgetc()

PHP读取文件-fread()

PHP fread()函数用于读取文件的数据。它需要两个参数:文件资源和文件大小。

句法

string fread (resource $handle , int $length )

$ handle表示由fopen()函数创建的文件指针。

$ length表示要读取的字节长度。

例子

<?php  
$filename = "c:\\file1.txt";  
$fp = fopen($filename, "r");//open file in read mode  

$contents = fread($fp, filesize($filename));//read file  

echo "<pre>$contents</pre>";//printing data of file
fclose($fp);//close file  
?>

输出

this is first line
this is another line
this is third line

PHP读取文件-fgets()

PHP fgets()函数用于从文件读取单行。

句法

string fgets ( resource $handle [, int $length ] )

例子

<?php  
$fp = fopen("c:\\file1.txt", "r");//open file in read mode  
echo fgets($fp);
fclose($fp);
?>

输出

this is first line

PHP读取文件-fgetc()

PHP fgetc()函数用于从文件读取单个字符。要使用fgetc()函数获取所有数据, 请在while循环内使用!feof()函数。

句法

string fgetc ( resource $handle )

例子

<?php  
$fp = fopen("c:\\file1.txt", "r");//open file in read mode  
while(!feof($fp)) {
  echo fgetc($fp);
}
fclose($fp);
?>

输出

this is first line this is another line this is third line

来源:

https://www.srcmini02.com/29380.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_37960.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?