码迷,mamicode.com
首页 > Web开发 > 详细

php写入数据到txt文件

时间:2016-01-07 19:57:40      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:

<form action="index.php" method="post">
    <input type="text" name="user"/>
    <input type="text" name="password"/>
    <input type="submit" value="submit"/>
</form>
<?php
    $user=trim($_POST[‘user‘]);
    $password=trim($_POST[‘password‘]);
    if(!$user || !$password){
        echo ‘please make sure no empty input‘;
        exit;
    }
    //write to test.txt
    $handle=fopen(‘test.txt‘,‘ab+‘);
    if($handle){
        $exits=false;
        while(($buffer=fgets($handle))!==false){
            $match=preg_match(‘/.*:(.*?)-/‘,$buffer,$array);
            //first write
            if(!$match){
                break;
            }

            //exist in txt
            if($match && $array[1]==$user){
                $exits=true;
            }
        }
    }else{
        echo ‘fail to open file‘;
    }

    if(!$exits){
        fwrite($handle,‘$user:‘.$user.‘-----‘.‘$password:‘.$password."\r\n");
    }else{
        echo ‘user exist‘;
    }
    fclose($handle);
?>

 

php写入数据到txt文件

标签:

原文地址:http://www.cnblogs.com/wz0107/p/5110879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!