php获取src,PHP读取文件
本文概述
PHP提供了各種功能來(lái)從文件讀取數(shù)據(jù)。有多種功能允許你讀取所有文件數(shù)據(jù), 逐行讀取數(shù)據(jù)以及逐字符讀取數(shù)據(jù)。
下面提供了可用的PHP文件讀取功能。
fread()
fgets()
fgetc()
PHP讀取文件-fread()
PHP fread()函數(shù)用于讀取文件的數(shù)據(jù)。它需要兩個(gè)參數(shù):文件資源和文件大小。
句法
string fread (resource $handle , int $length )
$ handle表示由fopen()函數(shù)創(chuàng)建的文件指針。
$ length表示要讀取的字節(jié)長(zhǎng)度。
例子
$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 filefclose($fp);//close file
?>
輸出
this is first line
this is another line
this is third line
PHP讀取文件-fgets()
PHP fgets()函數(shù)用于從文件讀取單行。
句法
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()函數(shù)用于從文件讀取單個(gè)字符。要使用fgetc()函數(shù)獲取所有數(shù)據(jù), 請(qǐng)?jiān)趙hile循環(huán)內(nèi)使用!feof()函數(shù)。
句法
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
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的php获取src,PHP读取文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: event php,PHP event
- 下一篇: php如何编写通信协议,定制通讯协议