skip to Main Content

I have 100 html files, and i want to add a line of code (at the end) to all of them.

How is this possible ?
Something like a script which appends that line into all html files

I tried searching but did not find anything.

2

Answers


  1. Your question is quite unclear, because you didn’t specify where you are going to append this lane? Here is a starting point:

    <?php
    
    $path = '/path/to/html/files';
    $code_to_add = '<p>This line was added by a script.</p>';
    
    $files = scandir($path); //  get an array of all the files in the directory specified by $path.
    foreach ($files as $file) { // Iterate over files
        if (substr($file, -5) == '.html') { // hecking if each file has a ".html" extension.
            $file_path = $path . '/' . $file;
            $file_contents = file_get_contents($file_path); // read the contents
            file_put_contents($file_path, $file_contents . $code_to_add); // put new content, you can modify it.
        }
    }
    
    ?>
    
    Login or Signup to reply.
  2. You can use :

    <?php
    $folder = '/path/to/html/files';
    $line_to_add = '<p>your line/p>';
    
    foreach (glob("$folder/*.html") as $file) {
        $contents = file_get_contents($file);
        file_put_contents($file, $line_to_add . PHP_EOL . $contents);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search