复制文件后更改php内容

时间:2020-04-09 18:48:57

标签: php

我在下面的代码中创建了一个新文件夹,并从另一个文件夹复制了一些文件,它工作正常。 现在,我需要转到新文件夹中的每个php文件,并通过$ _POST [“ variable”]传递的某个单词来更改“ empty”一词。 也许是通过str_replace来实现的,但是我陷入了逻辑困境。 预先感谢!

    if (!file_exists("/home//public_html/new_conf_folder/")) 
{
      mkdir("/home//public_html/new_conf_folder/", 0755, true);

      $source = "/home/public_html/conf_folder/";

      $destination = "/home/public_html/new_conf_folder";

      $directory = opendir($source);

      while(($file = readdir($directory)) != false) 
      { 

        copy($source.'/' .$file, $destination.'/'.$file);

      } 
    }

1 个答案:

答案 0 :(得分:2)

尝试使用此代码对我有用:

// file_process.php
$destination="./"; // Current Directory
if (file_exists($destination)) 
{  
      $directory = opendir($destination);
      while(($file = readdir($directory)) != false) 
      { 
        //echo $file."<br/>";
        $contents=file_get_contents($file);
        $contents=str_replace("\"empty\"",$_POST['variable'],$contents);
        //$contents=str_replace("\"empty\"",$_POST['variable'],$contents); // use this if the word with qouts
        //echo $contents;
        $bytes_written=file_put_contents($file,$contents);
        if($bytes_written>0)echo "File [$file] has been successfully processed.";
        else echo "Process Failed.";
      } 
}
相关问题