Automatically insert comments on every file in a script

Discussion in 'PHP' started by JWRmedia, Nov 3, 2008.

  1. #1
    Does anyone know of a small application or easy way to ne able to add a bit of code on every page of your script?

    For example, say I wrote a script containing 100 php files and I would like to add something like

    //My script name
    //Version 1.2.2
    Code (markup):
    At the top pf each page.

    There must be some way of doing this easily without hand coding it into all 100 script files.

    Any suggestions?
     
    JWRmedia, Nov 3, 2008 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    make a new php file, put this in it, and then save it to the directory containing all of the scripts
    When you run it, it will find the first <?php in each php file in that directory, and add your comment directly below it

    Make sure you use // or /* */ or # for your comments :)
    <?php
    
    $comment = '//My script name
    //Version 1.2.2';
    
    foreach(glob('*.php') as $file)
    {
        if($file == basename(__FILE__)) continue;
        $contents = file_get_contents($file);
        $contents = preg_replace('/<\?php/', "<?php\r\n".$comment, $contents,1);
        file_put_contents($file, $contents);
    }
    PHP:
    Jay
     
    JAY6390, Nov 3, 2008 IP
  3. Bind

    Bind Peon

    Messages:
    70
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if you have sub-directories you can use RecursiveDirectoryIterator() and RecursiveIteratorIterator() to romp thru all files in all directories from $dir downward.

    
    <?php
        $dir = "/server/path/to/start/from/";
        $comment1 = "// My script name";
        $comment2 = "// Version 1.2.2";
    ###########################################################
        $successful=0;
        $unsuccessful=0;
            $iterator = new RecursiveDirectoryIterator($dir);
            foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file)
                {
                    if ($file->isFile())
                        {
                            if ((substr($file, strlen($file) - strlen("php"), strlen("php"))) == "php")
                                {
                                    $importedfile = explode("\n", file_get_contents($file->getPathname()));
                                    $importedfile[0] = $importedfile[0] . "\n$comment1\n$comment2\n";
                                    if (@file_put_contents($file->getPathname(), implode("\n", $importedfile), LOCK_EX))
                                        {
                                            $successful++;
                                        }
                                    else
                                        {
                                            $unsuccessful++;
                                            $unsuccessful_text .= "ERROR (check permissions): " . $file->getPathname() . "<BR> - file alter unsuccessful<BR><BR>";
                                        }
                                }
                        }
                }
    echo "Successfully altered $successful files.<BR>";
    echo "Unsuccessfully altered $unsuccessful files.<BR>";
    echo "ERRORS: <BR><BR>$unsuccessful_text";
    ?>
    PHP:
    script places your comment on line 2 and 3 of each php file
     
    Bind, Nov 3, 2008 IP
  4. JWRmedia

    JWRmedia Banned

    Messages:
    499
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #4

    Can I name the file whatever I want, and how to I actually run the file? Just upload all the PHP files and click on this file to run?
     
    JWRmedia, Nov 5, 2008 IP
  5. Bind

    Bind Peon

    Messages:
    70
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    1. alter the $dir value for the path to the script root of your application your want to alter. This value should be available in your control panel. (example: $dir="/www/your_account/public_html/your_application/";) ... its only an example as yours will differ.

    2. upload the file to your applications root folder and name it anything you want.

    3. then jsut access the file in a browser.
     
    Bind, Nov 5, 2008 IP