PHP file_put_contents() 函数是一次性向文件写入字符串或追加字符串内容的最合适选择。

file_put_contents()

file_put_contents() 函数用于把字符串写入文件,成功返回写入到文件内数据的字节数,失败则返回 FALSE。

file_put_contents() 的行为实际上等于依次调用 fopen()fwrite() 以及 fclose() 功能一样。

 

在php中,用file_put_contents() 把字符串写入文件时,有时候发现写入的内容不能换行。

如:

<?php
$a = '<?php \n \$a["1"]=a; \n \$a["2"]=b; \n return \$a; \n';
file_put_contents('test.php', $a);
?>

test.php 的结果是:

<?php \n \$a["1"]=a; \n \$a["2"]=b; \n return \$a; \n

内容中没有回车。

在网上查找解决方法,说是要把输入的内容用""来代替''。试验了下,结果可以。

<?php
$a = "<?php \n \$a['1']=a; \n \$a['2']=b; \n return \$a; \n";
file_put_contents('test.php', $a);

?>

test.php 的结果是:

<?php 
 $a['1']=a; 
 $a['2']=b; 
 return $a; 

 

 

相关文章:

  • 2021-10-15
  • 2022-12-23
  • 2022-12-23
  • 2021-08-07
  • 2022-12-23
  • 2022-01-21
  • 2021-11-25
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案