博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

PHP 文件编程(三)

Posted on 2013-05-12 02:19  Step at a time  阅读(233)  评论(0编辑  收藏  举报

1、写文件

<?php

    //写文件
    $file_path="text.txt";
    if(!file_exists($file_path)){
        echo "文件不存在";
        exit();
    }
    //"a+" 在文件后面追加  "w+"重新写入

    $fp=fopen($file_path,"w+");
    $con="\r\n你好";
    for($i=0;$i<10;$i++){
    fwrite($fp,$con);}

    echo "添加成功";
    fclose($fp);

    
?>

2、第二中方式  通过file_put_contents函数

<?php


    //第二种方式写文件
    $file_path="text.txt";
    $content="hello,world\r\n";

    //将一个字符串写入文件  默认是【FILE_USE_INCLUDE_PATH 】"w+"重新写入
    file_put_contents($file_path,$content,FILE_APPEND);

    echo "OK";
?>