708   PHP

1,fgets() 函数用于从文件中读取一行数据,并将文件指针指向下一行,适合一行行读取文件内容

$handle = fopen('test.txt', 'r');
while(!feof($handle)){
    echo fgets($handle);
}
fclose($handle);

2,fread() 函数读取指定字节数内容到字符串

$file = fopen("test.txt","r");
//求出文件字节数
$filesize = filesize("test.txt");
$str1 = fread($file,$filesize);
$str2 = fread($file,10);
fclose($file);

3,file()函数将整个文件读入,并一行行放入对应数组中,适合一次读取文件到数组;支持url读取

$file = file('test.txt');
foreach($file as $line => $content){
    echo 'line '.($line + 1).':'.$content;
}

4,file_get_contents()函数一次性读取文件内容到字符串中,可指定读取字符串开始位置和字节数;支持url读取

$content = file_get_contents('test.txt',false,null,2,7);
var_dump($content);



Leave a Reply

Your email address will not be published. Required fields are marked *