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
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
<title>my ajax file written with php</title>\n
if (xmlhttp.readyState==4 && xmlhttp.status==200)\n
<button type=\"button\" onclick=\"loadit()\">Change Text</button>\n
$a = fopen("my_ajax_file_written_with_php.html", 'w');