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?
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
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
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?
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.