<?php //第一种方式读取文件 $file_path = "db.ini"; //判断文件是否存在(必须判断) if(file_exists($file_path)){ //打开文件,指定文件路径,读取方式 $fp = fopen($file_path, "a+"); //读取长度 $con = fread($fp,filesize($file_path)); //替换字符 //php不会对特殊字符做出处理,需要自己去转换 $con = str_replace("\n","<br/>",$con); echo $con; }else{ echo "文件不存在"; } //关闭文件,必须 fclose($fp); //第二种方式读取文件,自动会关闭文件 //直接获取文件内容 //判断文件是否存在(必须判断) if(file_exists($file_path)){ //直接获取文件内容 $con = file_get_contents($file_path); //替换字符 $con = str_replace("\n","<br/>",$con); echo $con; }else{ echo "文件不存在"; } //自己会关闭文件 //第三方式读取文件 //while循环读取,适合大文件 //判断文件是否存在(必须判断) if(file_exists($file_path)){ //打开文件 $fp = fopen($file_path,"a+"); $str = ""; $buffer = 1024; //while循环读取,适合大文件 while(!feof($fp)){ $str = fread($fp,$buffer); $str = str_replace("\n","<br/>",$str); echo $str; } //关闭文件,必须 fclose($fp); }else{ echo "文件不存在"; } //第四种读文件(自动会关闭文件) if(file_exists($file_path)){ $arr = parse_ini_file("db.ini"); print_r($arr); //mysql_connect($arr['host'],$arr['user'],$arr['password']); }else{ echo "文件不存在"; } ?> </body> </html>