Write Nearly Any Type of File with PHP
PHP can write just about anything. The only type of file missing from the list below is PDF file, and, yes, PHP can write those too if your host server's PHP installation has the PDFlib library installed. Our host doesn't have it, but the above link will tell you what you need to know. If you need PHP to write DOC, XLS, or RTF files, try http://www.ozzu.com/programming-forum/php-create-word-document-t65376.html. See below for just about everything else.
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. Here is what to do if you are testing a PHP script for writing a file and you get the dreaded permission error message:
- Close your browser and FTP program and restart them.
- Before going to your PHP script page, clear your browser cache, which is found in different places depending on which browser. On IE8 it is in Developer Tools on the Tools menu, and on IE6, on the Tools menu, click Internet Options, which opens to the General tab. In the Temporary Internet Files section, click the Delete Files button. In Firefox, on the Tools menu choose Options, then the Network tab. Click Clear Now in the Offline Storage section.
- In your FTP program, reset the appropriate permissions, which may also be referred to as CHMOD. In SmartFTP, for instance, right click the public_html folder and select Properties and replace the current number in the permissions box with 755. Then do the same with your script's folder, and then do the same with the script file itself.
- Clear the cache one more time for good luck.
- Get to your script page and load the page in the online browser, then reload it again with F5 for good luck.
- If the script will not fwrite, and you see permissions errors, do all the previous steps again, substituting 777 in place of 755 for CHMOD settings.
- If still no go on getting the PHP fwrite function to write, ask your server host if he has either of these two things disabled: fwrite or similar PHP file creation functions, or permissions beyond 755 which allow Other or Group to Write (755 allows only User—you—to Write). If he tells you it is for security, he is correct: file creation functions are a bit iffy in the security department. See if there is any way you can get the disablement undone at least temporarily. But if you need to be able to do this type of thing but the host won't budge, your only option is to either find a host who is willing to let you do it, or install Apache and then install PHP on your hard drive and host it yourself. You will also need to find out if your internet service provider (ISP) will allow you to run websites on your home computer. If not, you have to use a different provider.
- Are you sure you need PHP file creation functions that much? :) :D lol
So why would anyone want to fwrite files anyway? There are a lot of file handling functions in PHP and plenty of places to learn about these filesystem functions. Reading, creating, copying, moving, uploading—there are plenty of file tasks that can need doing. And while most of us simply do these with our Windows Explorer or 2xExplorer file management programs and Notepad or Editor2 (our favorite), or FTP programs like SmartFTP, sometimes we need to do it on the fly online, or empower others to do it online. The above links lead to dozens of file functions that are in servers' PHP installations for our use.
But 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