php 数据写入txt时的问题

WBOY
Release: 2016-06-23 14:04:37
Original
1126 people have browsed it

$filename='users.txt';
$name = trim($_POST["name"]);
$passwd = trim($_POST["pass"]);
$str=$username.'|'.$passwd;
$suc=file_write($str,$filename);
function file_write($flag,$tmpfname)
{   
   if($handle = fopen($tmpfname, "a")) 
{      
   fwrite($handle, $flag."\r\n");    
   fclose($handle);     
   return 1;    
 }      
 else return 0;    
 }
把名字和密码记录在txt上,格式是mmm|123456
                          yyy|123456
我的代码如上,为什么第一次写入时总会空出一行,不管是空白文本还是已经有数据的。而且有时候写入空白文本时,不能成功,求大神们帮我优化下


回复讨论(解决方案)

经测试,没有发现你说的现象
尽管你的变量名写错了

经测试,没有发现你说的现象
尽管你的变量名写错了
晕啊,我试了下好像也是对的,那肯定是我验证名字是否已存在这部分错了。。。
斑竹能帮我看下吗
$Logi = file ( "users.txt" ); 
$size = sizeof ( $Logi );
foreach( $Logi as $Key => $Val ) 
{ $Data [ $Key ] = explode ( "|" , $Val );
  

   for( $K = 0 ; $K  { 
  $user = $Data [ $K ][ 0 ];
  if($user==$_POST[ "name" ])
{
   echo "This name already exits.Please get another one.";
   break;
  }
 
 if($K==$size-1)
{$suc=file_write($str,$filename);}
 }

直接用file_put_contents()多简便呀!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!