YYT15752210557 2019-01-27 14:23 采纳率: 0%
浏览 472
已采纳

str_replace替换目标文件内容,结果目标文件全被清空了,为什么

<?php
$a=fopen(dirname(__FILE__)."/index.php","w");
echo dirname(__FILE__)."/index.php";
str_replace("你好","helllo",$a);
fclose($a);

  • 写回答

3条回答 默认 最新

  • 锅巴胸 2019-01-28 10:38
    关注
    <?php
    //$a=fopen(dirname(__FILE__)."/index.php","w");
    $a = dirname(__FILE__)."/index.php";
    file_put_contents($a,str_replace('你好','helllo',file_get_contents($a)));
    //fclose($a);
    

    帮你稍微改了一下,清空了是因为你只是打开了文件并没有读取文件里的内容哈。
    效果:图片说明

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?