PHP Script for Writing PHP Web Page Files
PHP can write just about anything. The script below will concentrate on PHP Web Page files.
The one thing you need to watch here is permissions. On some servers, you need permissions (CHMOD) set to 755 to use the PHP fwrite function, which all the file creations below use in their code. But on other servers (like ours, we discovered), you need permissions set to 777. There are people that understand why. We are not those people, nor is our server host. Older servers seem to need 777. Newer servers need only 755—which is obviously more secure. But this is not a hard and fast rule—your mileage may vary. If you find that you need 777, you may find your host blocks this setting. But you may find you can fwrite with only a 755 CHMOD (public_html AND the PHP script file setting). Some hosts disallow fwrite and even PHP file functions in general. You will find that write-nearly-any-type-of-file-with-php.html is the best place you can go for answers if you are testing a PHP script for writing a file and you get the dreaded permission error message.
There are times when you just need to use PHP to write various types of files. The list below shows you how—it's not hard at all. There are even five PHP scripts for creating graphics/image files. For image creation, we concentrated on the simple task of allowing users to input text and using PHP to take this text and put it in a nice box with a border and save it as a graphics/image file. Let us know how you like these scripts! We kept them as simple as we could so you could get the idea in a flash.
- Write Text File with PHP
- Write JS File with PHP
- Write PHP File with PHP
- Write HTML File with PHP
- Write ASP File with PHP
- Write ASPX File with PHP
- Write XHTML File with PHP
- Write XML File with PHP
- Write AJAX File with PHP
- Write CSS File with PHP
- Write Flash Web Page File with PHP
- Write MP3 Web Page File with PHP
- Write YouTube Video Web Page File with PHP
- Write Windows Media Video Web Page File with PHP
- Write MPEG Video Web Page File with PHP
- Write PNG File with PHP
- Write GIF File with PHP
- Write JPEG File with PHP
- Write WBMP File with PHP
- Write XBM File with PHP
First, notice that we used escaped double quotes inside the $p string since unescaped double quotes would conflict with the double quotes the $p string is surrounded by. And of course, the newline characters are escaped (\n). We will look at PHP in a second. But first, let's check out the PHP aspects of the script. The $p variable is most of the script—it's the PHP page content. After the closing tag for PHP is a " character. That is the end of the PHP string with the variable name $p. Next we use fopen and put in a file name and a w for write. Then we use fwrite to write the file. Next we use fclose, which you should always do when using PHP file functions. Finally we CHMOD the permissions to a nice safe 755. A created file may be getting this CHMOD by default anyway, but why take chances? Of course, your host may not allow CHMOD from a PHP file anyway. Some do; others don't.
So that takes care of the PHP, which simply writes a PHP file whose contents are what's in the PHP variable string $p.
If you want to learn PHP because you do not know it, check out an PHP tutorial.
According to Wikipedia, "In dynamic sites, page content and page layout are created separately. The content is retrieved from a database and is placed on a web page only when needed." There are a lot of ways to create DHTML pages. One can have the dynamics in the display and page changes can be the result of page events like button clicking or mouse hovering. The changes can occur in any CSS styles: for example, font sizes, font styles, or paragraph style and spacing like in cms-read-articles.html, where changes are accomplished by looping through all the paragraph element tags and changing style properties. On this same page, dynamic page creation occurs when the user types the article name into an HTML form and the script retrieves the article from a MySQL database table and the data is echoed to the screen via PHP. Even though an unlimited number of articles can be presented on this PHP page, the page itself is merely getting its content changed—it's still the same PHP page. So, on this sample page, page content and page layout are both being changed on the fly, based on user choices. This is the essense of a dynamic web page. Note that no permanent page writing or use of the PHP file functions—which can be security risks—are needed or wanted.
All this is not to dissuade you from creating any PHP pages on the fly using PHP file functions. You may easily find some use for this idea in archive creation or general database use. In databases--mysql-or-not-mysql.html, we discuss the subject of databases in fair depth. Of flat (text) file databases, SQLite usually will work great as the database engine of choice. One file holds the entire database. After db creation, everything else is just file editing/reading. Our write-text-file-with-php.html script is a very simple example of writing a flat/text file and if you want a flat/text file database that's actually usable, a really simple flat file database with column sorting can be found here: Flat File Database Demo. But for a serious non-MySQL database, SQLite is the way to go. All of this is file writing. We've even seen a blog that saves HTML pages on the fly, but we cannot recall whether the scripter's purpose was archival, SEO, or what. Obviously Wordpress and Blogger use saved blog content, but it's saved in MySQL for Wordpress and who-knows-what for Blogger. I.e., like with the articles site mentioned in the last paragraph, the blog contents are stored in databases and retrieved as dynamic web page contents. They don't save HTML pages.
Here is the result: my_php_file_written_with_php.php
$p = "<?php\n echo \"This is a PHP page. How do you like it?\";\n ?>";
$a = fopen("my_php_file_written_with_php.php", 'w');