复制文件后更改php内容

复制文件后更改php内容,php,Php,我有下面的代码,我正在创建一个新文件夹,并从另一个文件夹复制一些文件,它工作正常。 现在,我需要处理新文件夹中的每个php文件,并将单词“empty”更改为通过$\u POST[“variable”]传递的某个单词。 也许是stru_替换,但我陷入了逻辑。 提前谢谢 if (!file_exists("/home//public_html/new_conf_folder/")) { mkdir("/home//public_html/new_conf_folder/", 0

我有下面的代码,我正在创建一个新文件夹,并从另一个文件夹复制一些文件,它工作正常。 现在,我需要处理新文件夹中的每个php文件,并将单词“empty”更改为通过$\u POST[“variable”]传递的某个单词。 也许是stru_替换,但我陷入了逻辑。 提前谢谢

    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);

      } 
    }

试试这段代码它对我有用:

// 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.";
      } 
}
//文件\u process.php
$destination=“./”;//当前目录
如果(文件_存在($destination))
{  
$directory=opendir($destination);
而(($file=readdir($directory))!=false)
{ 
//echo$文件。“
”; $contents=file\u get\u contents($file); $contents=str\u replace(“\'empty\”,$\u POST['variable'],$contents); //$contents=str\u replace(“\'empty\”,$\u POST['variable'],$contents);//如果单词有qouts,则使用此选项 //echo$内容; $bytes\u write=文件内容($file,$contents); 如果($bytes\u write>0)echo“文件[$File]已成功处理。”; else echo“进程失败。”; } }
我想您已经在这里处理了代码注入的风险。您好@symcbean,请问是哪种风险?我读过一些关于symfony的书。更好吗?在学习基础语言/技术之前,不要尝试使用框架。如果用户对目标文件名有任何控制权,他们可以将自己喜欢的任何代码注入该文件,然后在您的服务器上执行。哦,是的!我有这个想法。用户无法知道这一切都是在幕后进行的。我只需要它来为每个用户创建文件夹。谢谢@user2448808,但是有点不对劲。它没有写文件。我编辑了你的代码,它工作并显示了更改的字符串,但是当我检查目录时,没有任何更改。错误日志告诉我msg file_put_contents(.):无法打开流:是目录我认为这是权限问题,请尝试将755权限放入目标文件夹并更改用户组。权限和用户都正常。这很奇怪。当我使用$destination=“./”;并将文件_process.php放在它工作的当前文件夹中。但是当我从外部使用时,甚至文件_process.php($contents=str_replace(“:empty:”,$word,$contents);)中的代码都在更改。谢谢@user2448808,我已经更改了文件_get_content和文件_put_content上的路径,它现在正在工作。我很感激!