PHP文件编程(二)-读取文件的四种方式
                        
                            时间:2021-07-01 10:21:17
                            帮助过:28人阅读
							                        
                     
                    
                    |     //读取文件    $file_path="text.txt";    if(!file_exists($file_path)){        echo "文件不存在";        exit();    }        //打开文件    $fp=fopen($file_path,"a+");    //读取文件    $content=fread($fp,filesize($file_path));    echo "文件内容是:";
    //默认情况下把内容输出到网页后,不会换行显示,因为网页不识别\r\n    //所有要把\r\n =>
        $content=str_replace("\r\n","",$content);
    echo  $content;    fclose($fp);?>
 2、读取文件的第二种方式
     //第二种读取文件的方式    $file_path="text.txt";    if(!file_exists($file_path)){        echo "文件不存在";        exit();    }    $content=file_get_contents($file_path);    $content=str_replace("\r\n","",$content);
    echo  $content;?>
 3、循环读取(对付大文件)的方式
      //第三种读取方法,循环读取(对付大文件)    $file_path="text.txt";    if(!file_exists($file_path)){        echo "文件不存在";        exit();    }    //打开文件    $fp=fopen($file_path,"a+");    //定义每次读取的多少字节    $buffer=1024;    //一边读取。一边判断是否达到文件末尾    while(!feof($fp)){        //按1024个字节读取数据        $content=fread($fp,$buffer);        echo $content;    }    fclose($fp);?>
 4、读取ini配置文件
1)、db.ini 文件
      $arr=parse_ini_file("db.ini");    echo "";    print_r($arr);    echo "";        echo $arr['host'];    //连接数据库    $conn = mysql_connect($arr['host'], $arr['user'], $arr['pwd']);    if(!$conn){        echo "error";    }    echo "OK";?>
 |