PHP Script to Convert Any Web Page File Extension

PHP



In this article, we show how to write a PHP script which can convert any web page file extension to another web page file extension in a directory.

This means that any .html files can be changed to .php files.

.php files can be changed to .html files.

.html files can be changed to .asp files.

.php files can be changed to .asp files.

And the list goes on and on and on and on. The point is, you can change any file extension with PHP pretty easily.

If you have hundreds or thousands of files in a directory, this could save hours, days, or weeks of manual work.

So how is this done?

Basically, the gist of it is that PHP is a powerful server side language. It can access and manipulate the backend of a website such as the directories that comprise a website. Once it access a directory, it can loop through each file in the directory. When it loops through each file in the directory, we can check the file extension of that file. If the file extension is .html, we can convert it to .php, changing the file from an HTML file to a PHP file. And we can convert any file extension from one to another.

Yes, this is the power of a server side language such as PHP.

We will go over this below.



PHP Script to Convert HTML Files (.html) to PHP Files (.php) Files

Okay, let's take one case first.

Let's convert HTML files (.html) to PHP files (.php).

So let's say you want to upgrade your website from static HTML to dynamic PHP pages, which offer much more power for your webpages.

So how can you loop through all the files in your website that you want to?

The PHP code to loop all files in your home directory and all files in the subdirectories of the home directory is shown below.



So this code shown above is very powerful. Be cautious. Use at your only risk. If you copy the above code, save it as a PHP file, and upload it to the root directory of your website, it will convert every HTML page to a PHP page. Only use it and upload it to your website if you actually want to convert every HTML file to a PHP file.

The code loops through every file in the home directory and every file in any subdirectory of the home directory. If the file is an HTML file, it converts the html file to a PHP file.

We'll now go over all the code so that you can understand.

So first, if you want to go all through the files on your website, the best way to do so is through the home directory. So we create a variable called $homedirectory and make it equal to "." This gives us the home directory. Now to say all the files in the home directory, we use the PHP scandir() function. We open the directory and use a while loop to loop through all files in the directory. We then use an if statement to check to see which of these files are directories. We open these directories through the opendir() function again. We then loop through all the files in these subdirectories. Since each of the subdirectories contains a backwards link to the home directory, it's as if the home directory is in the subdirectories. So we're able to loop through all the files in the home directory and all files in the subdirectories of the home directory.

We then use an if statement that if the file has an .html extension, we use the str_replace() function to replace the .html file extension with .php file extension. We then use this new replaced string to rename the file to this new name. Being that we're going to upload this PHP script to the root directory of our website, when using the PHP rename() function, we must make it relative to the home directory. Therefore, we have to put, for example, subdirectory/file_name.file_extension in order for the rename() function to work. This is what we do in the code. Otherwise, it will not work. The rename() function script cannot be uploaded to one directory and made it work in another directory without specifying the path to the directory that you want to rename a file.

So after the file is now renamed, we echo out which files have been changed by this update.


If you don't want to convert every HTML file on your website to a PHP file but instead just all HTML files in a certain directory, then you would use the code below, changing the $directory variable to the directory that you want.



So you can see that this code above is a lot simpler than the previous one that loops through all the directories in the home directory.

Change the $directory variable to the directory that you want to convert the files.

Other than that, most things else is the same.

This, again, converts all HTML files in the directory you specify to PHP files.



PHP Script to Convert PHP Files (.html) to HTML Files (.php) Files

So now we'll do the reverse. Below is the code to convert all PHP files in the root directory and all subdirectories in the root directory on your website to HTML pages.



Everything is the same as the previous code but now we search for PHP files and use the str_replace() function to replace the .php extension with the .html extension.



PHP Script to Convert PHP Files (.html) to ASP Files (.asp) Files

And we really can convert any file extension to any other file extension with this PHP code.

Below is the code to convert all PHP files in the root directory and all subdirectories in the root directory on your website to ASP (active server pages) pages.



To convert any file extension to any file extension with PHP, all you have to do is manipulate the if statement that searches the string (this is the if statement that has the PHP strpos() function. And you just have to manipulate the $file_rename variable via the PHP str_replace() function. Once you do this, you can convert any file name to any file name.



PHP Script to Convert ASP Files (.asp) to PHP Files (.php) Files

Below is the code to convert all ASP files in the root directory and all subdirectories in the root directory on your website to PHP pages.



And you can do this for any files. You probably get the point now.

You dont simply have to convert file names just matching purely from the perspective of file names. You can convert file names to another if the file has something in it. For example, you want to convert all files on your website that have the name HTML4 in it and is an HTML file to a PHP file. In that case you would add an AND statement to the if statement containing the PHP strpos() function to include this. You can do plenty of modifications.

So yeah, this code is very powerful. Be aware again. It's a great script for converting a large batch of files quickly to another type of file.

Be aware, though, that if you convert these files to another file type, this could reap disaster on your website. If you have links all over the pages of your websites, these links won't be changed by this update. This means this could produce a wide range of broken links on your site (404 errors) that could render your site largely unusable. So be aware of this. To change all the hyperlinks on your site, you would need to create a spider to crawl your site and change the links. I haven't created a tutorial on that yet. But keep this in mind.



HTML Comment Box is loading comments...