1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > php获取src PHP读取文件

php获取src PHP读取文件

时间:2022-05-31 11:28:08

相关推荐

php获取src PHP读取文件

本文概述

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

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

fread()

fgets()

fgetc()

PHP读取文件-fread()

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

句法

string fread (resource $handle , int $length )

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

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

例子

$filename = "c:\file1.txt";

$fp = fopen($filename, "r");//open file in read mode

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

echo "

$contents

";//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 ] )

例子

$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 )

例子

$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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。