PHP Script for Writing AJAX Web Page Files
PHP can write just about anything. The script below will concentrate on AJAX website pages.
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 escaped all the double quotes inside the $p string so as not to conflict with the double quotes the $p string is surrounded by. And of course, the newline characters are escaped (\n). In the script, we will have a button with the words "Change Text" on it and the word "Change!" above it. We also have a text file called ajax.txt which contains only the phrase "See, it changed!" The simple script lets users push the button and the word "Change!" will change to the phrase "See, it changed!" because the Ajax script will read the text file, grab the contents, and use them as a replacement for the word "Change!" on our web page. Cooler yet, the page will not have to do any reloading in the process of going and getting text from a separate online page and using it to edit the current page's content. True, the same trick could be performed with a simple JavaScript function without page reload and without recourse to an exterior page. But it would work only if the current web page had prior knowledge of what the needed change was. What if it didn't have a clue what the change was to be and only the ajax.txt page contained that info? For that matter, what if the info was only to be found in an online MySQL database table which was being updated and read by Ajax scripts every 8 seconds? That is the exact scenario in our Ajax chat room script.
Anyway, the current script will write an HTML page featuring the resplendant glories of Ajax, which uses JavaScript and XMLHTTP. But first, let's check out the PHP aspects of the script. The $p variable is most of the script (22 lines long!).
After the closing tag for HTML 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 644. 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 an HTML file whose contents are what's in the PHP variable string $p. The rest of the script is standard HTML stuff and then a nice big JavaScript function in the head section between script tags. (If you'd like to explore Ajax further, try Ajax—there are some great tutorials there.)
What is Ajax? AJAX means Asynchronous JavaScript and XML, but it isn't a programming language. It is a new way to use existing languages. When one uses Ajax, one is exchanging data with a server and updating parts of a web page, but without reloading the whole page! For a great example of Ajax in action, see http://www.w3schools.com/Ajax/ajax_aspphp.asp or http://www.css-resources.com/Ajax-and-PHP-Based-Insult-Auto-Completer.html. Happily, one need not learn XML or even XHTML to use Ajax, although there's a whole ton one can do if one does learn those. But those are not of interest to us currently so let's stay with the problem at hand. Incidentally, Ajax can be on regular HTML or PHP pages—you need not go to XML or XHTML.
On to the code: Notice the div with the id "myCoolDiv". This is where the "Change!" word will be. When the Ajax routine runs, using XML to consult the text file, it gets back its xmlhttp.responseText from this text file, and it stores it in the innerHTML of the div with the id "myCoolDiv". (To see the script without all the annoying backslashes, click on the link below, run the script, and view the source code.) Once the text file text is stored in our web page's div, the user now sees this text, and the original page text is gone forever unless you refresh the page with F5. If you want more info on what the xmlhttp stuff is, check out Ajax.
Here is the result: my_ajax_file_written_with_php.html
<?php
$p="<html>\n
<head>\n
<title>my ajax file written with php</title>\n
<script type=\"text/javascript\">\n
function loadit(){\n
if (window.XMLHttpRequest)\n
{xmlhttp=new XMLHttpRequest();}\n
else\n
{xmlhttp=new ActiveXObject(\"Microsoft.XMLHTTP\");}\n
xmlhttp.onreadystatechange=function(){\n
if (xmlhttp.readyState==4 && xmlhttp.status==200)\n
{document.getElementById(\"myCoolDiv\").innerHTML=xmlhttp.responseText;}}\n
xmlhttp.open(\"GET\",\"ajax.txt\",true);\n
xmlhttp.send();}\n
</script>\n
</head>\n
<body>\n
<div id=\"myCoolDiv\"><h1>Change!</h1></div>\n
<button type=\"button\" onclick=\"loadit()\">Change Text</button>\n
</body>\n
</html>\n";
$a = fopen("my_ajax_file_written_with_php.html", 'w');
fwrite($a, $p);
fclose($a);
chmod("my_ajax_file_written_with_php.html", 0644);
?>