PHP 文件编程(二)   
               添加时间:2013-5-14 点击量: 
 
              1、读取文件操纵
<?php
    //读取文件
    ¥file_path=text.txt;
    if(!file_exists(¥file_path)){
        echo 文件不存在;
        exit();
    }
    
    //打开文件
    ¥fp=fopen(¥file_path,a+);
    //读取文件
    ¥content=fread(¥fp,filesize(¥file_path));
    echo 文件内容是:<br/>;
    //默认景象下把内容输出到网页后,不会换行显示,因为网页不辨认\r\n
    //所有要把\r\n =><br/>
    
    ¥content=str_replace(\r\n,<br/>,¥content);
    echo  ¥content;
    fclose(¥fp);
?>
2、第二种读取文件的体式格式
<?php
    //第二种读取文件的体式格式
    ¥file_path=text.txt;
    if(!file_exists(¥file_path)){
        echo 文件不存在;
        exit();
    }
    ¥content=file_get_contents(¥file_path);
    ¥content=str_replace(\r\n,<br/>,¥content);
    echo  ¥content;
?>
3、第三种读取办法,轮回读取(对于大文件)
<?php
    //第三种读取办法,轮回读取(对于大文件)
    ¥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、文件读取实际应用:连接数据库的时辰,可以把指定的数据设备到一个文件中,然后再PHP运行时,及时获取信息
db.ini 文件
host=127.0.0.1
user=root
pwd=root
db=test
获取文件
<?php
    ¥arr=parse_ini_file(db.ini);
    echo <pre>;
    print_r(¥arr);
    echo </pre>;
    
    echo ¥arr[host];
    //连接数据库
    ¥conn = mysql_connect(¥arr[host], ¥arr[user], ¥arr[pwd]);
    if(!¥conn){
        echo error;
    }
    echo OK;
?>
原来,再大的房子,再大的床,没有相爱的人陪伴,都只是冰冷的物质。而如果身边有爱人陪伴,即使房子小,床小,也觉得无关紧要,因为这些物质上面有了爱的温度,成了家的元素。—— 何珞《婚房》#书摘#
                     
                  
     
  
 
    
    
1、读取文件操纵
<?php
//读取文件
¥file_path=text.txt;
if(!file_exists(¥file_path)){
echo 文件不存在;
exit();
}
//打开文件
¥fp=fopen(¥file_path,a+);
//读取文件
¥content=fread(¥fp,filesize(¥file_path));
echo 文件内容是:<br/>;
//默认景象下把内容输出到网页后,不会换行显示,因为网页不辨认\r\n
//所有要把\r\n =><br/>
¥content=str_replace(\r\n,<br/>,¥content);
echo ¥content;
fclose(¥fp);
?>
2、第二种读取文件的体式格式
<?php
//第二种读取文件的体式格式
¥file_path=text.txt;
if(!file_exists(¥file_path)){
echo 文件不存在;
exit();
}
¥content=file_get_contents(¥file_path);
¥content=str_replace(\r\n,<br/>,¥content);
echo ¥content;
?>
3、第三种读取办法,轮回读取(对于大文件)
<?php
//第三种读取办法,轮回读取(对于大文件)
¥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、文件读取实际应用:连接数据库的时辰,可以把指定的数据设备到一个文件中,然后再PHP运行时,及时获取信息
db.ini 文件
host=127.0.0.1
user=root
pwd=root
db=test
获取文件
<?php
¥arr=parse_ini_file(db.ini);
echo <pre>;
print_r(¥arr);
echo </pre>;
echo ¥arr[host];
//连接数据库
¥conn = mysql_connect(¥arr[host], ¥arr[user], ¥arr[pwd]);
if(!¥conn){
echo error;
}
echo OK;
?>
原来,再大的房子,再大的床,没有相爱的人陪伴,都只是冰冷的物质。而如果身边有爱人陪伴,即使房子小,床小,也觉得无关紧要,因为这些物质上面有了爱的温度,成了家的元素。—— 何珞《婚房》#书摘#




