PHP 文件操作第 文件读取

本文详细介绍了使用PHP进行文件读取的方法,包括fopen、fread、filesize及fclose等函数的应用,以及如何处理大文件。同时,还介绍了如何使用parse_ini_file函数解析INI配置文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件读取 fread 函数 参数$fd,$file_size;

获取文件的大小 filesize($path); 参数$path 文件路径;

字符串替换 str_replace("\r\n","<br>",$con_str);

<?php
header("content-type:text/html;charset=utf-8");

#1. 定义文件路径
$file_full_path = "F:/test.txt";
if(file_exists($file_full_path)){

    #2.打开文件fopen
    $fd = fopen($file_full_path,'r');
   
    #3.读取文件大小
    $file_size = filesize($file_full_path);
    $con_str = fread($fd,$file_size);
    //及时关闭文件!!!重要 数据采集 MFC 
    fclose($fp);
    // str_replace 正则替换字符串的。。
    $con_str = str_replace("\r\n","<br>", $con_str);
    #4.读取文件
    echo  $con_str;
}

文件过大处理方法 feof($fd);

while()

<?php
header("content-type:text/html;charset=utf-8");

#1. 定义文件路径
$file_full_path = "F:/test.txt";
if(file_exists($file_full_path)){

    #2.打开文件fopen
    $fd = fopen($file_full_path,'r');
    #3.读取文件大小
    $file_size = filesize($file_full_path);
    $buffer = '';
    $buffer_size = 1024;
    $con_str = '';
    while (!feof($fd)){
        $buffer = fread($fd,$buffer_size);
        $con_str = $buffer.$con_str;
    }
    //及时关闭文件!!!重要 数据采集 MFC 
    fclose($fd);
    // str_replace 正则替换字符串的。。
    $con_str = str_replace("\r\n","<br>", $con_str);
    #4.读取文件
    echo  $con_str;
}

配置文件 ini 用法

函数 pase_init_file($filepath); 返回值是数组

<?php
header("content-type:text/html;charset=utf-8");

$file_path = 'config.ini';
$arr_ini = parse_ini_file($file_path);
var_dump($arr_ini);

echo "<br>用户名".$arr_ini['user'];
echo "<br>密码".$arr_ini['password'];

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值