user13054316
user13054316

Reputation:

Changing php content after copy files

I have the code bellow where I'm creating a new folder and copying some files from another folder, and it's working fine. Now I need to go on each php file inside new folder and change the word "empty" by some word passed through $_POST["variable"]. Maybe by str_replace but I'm stuck in the logic. Thanks in advance!

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

      } 
    }

Upvotes: 0

Views: 67

Answers (1)

Rani
Rani

Reputation: 490

Try this code its working for me:

// 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.";
      } 
}

Upvotes: 1

Related Questions