Previous Index Next

PHP Script

August 28th, 2019

PHP is a server side scripting language. Kinsta hosting has an explanation of PHP use in Wordpress. I don't use Wordpress, but I use PHP in similar ways on my website.

Recently I wanted to convert a website that used PHP to a static HTML site. I created a PHP script that will recurse through my directory tree, finding each PHP file. When a PHP file is found, my script appends and prepends code. That code will convert the file to a static HTML file when the PHP file is loaded in a browser. I was working with around 80 PHP files in many subdirectories.

There are other ways to accomplish this task. I could have used a text editor to add the code one file at a time. That would have been quicker than putting a PHP script together, at least for me, but it would have been tedious. I know because I've done that sort of thing.

The search and replace function of a text editor such as Notepad++ would do the job. It would have taken less time to find the correct Regex statement for the search. When I finished my PHP script, I tried Notepad++ and proved this to be true.

I worked locally on a WAMP server. Working with PHP is difficult without a local server. Writing a PHP script required finding examples of code that would do part of the task. I needed to understand enough to patch and paste some code together. There was a lot of trial and error. That's where the local server makes a huge difference. Without it, for each trial, I would have to FTP the script to a copy of the website on a remote server.

The script is below this narrative. I doubt that anyone will find and use it. If you are that person, please TEST on a copy of your target and make sure you are happy with the results.

As is, the script will echo all the PHP files in a directory and its sub-directories. You will have to find the line that includes "your path to a directory" and insert your own path to the directory of your choice. I placed my script in the same directory. For me it was the root directory of the website.

To append and prepend, remove the commented text inside the 'foreach' statement.
When you finish running the script, all PHP files in the path directory and its subdirectories will have a line of PHP code added to their beginning and ending. Loading the PHP file into a browser will output a static HTML file named index.html. If you have PHP files that are not named 'index.php', use a text editor to change the appended text before loading the PHP file in your browser.

Download Recurse Script
signature Phil W. Lowder