php的函数file()的用法,该如何解决

WBOY
Release: 2016-06-13 13:39:49
Original
955 people have browsed it

php的函数file()的用法
  $fp=fopen("交响乐.txt","rb+");
  fseek($fp,0);
  while($fp){
  echo fgets($fp);echo "
";
  }
  echo "


";
   
  $readarray=file("经典时尚语录.txt");
  if(!$readarray){
  echo "用file函数读取文件失败
";
  }
  else file_put_contents("语录.txt",$readarray);  
  echo "
";
  fclose($fp);
?>
上述代码中为什么不能实现将同一目录下的文件"经典时尚语录.txt"的数据写到文件"语录.txt"(新创建的)中去?

我不解也?请各位指点,不胜感激!

------解决方案--------------------
$fp=fopen("交响乐.txt","rb+");
fseek($fp,0);
while( feof($fp) ){ 
echo fgets($fp);echo "
";
}

fgets()只取文件中 一行 内容。取哪行,由文件内部指针确定。fgets每取一行内容,指针向前一行。
feof()用于检测指针是否已经到底,即文件最末尾。
而你用$fp,它的值并不会变化,因此会陷入死循环

===========
$readarray=file("经典时尚语录.txt");
if(!$readarray){
echo "用file函数读取文件失败
";
}
else file_put_contents("语录.txt",$readarray);
echo "
";
fclose($fp);

file()函数的返回值是数组类型。而file_put_contents()接受的类型是字符串,因此,写入的的$readarray会被强制类型转换为字符串类型,也就是'Array'
参考:http://www.php.net/manual/zh/ref.filesystem.php
Related labels:
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!