PHP Script for Writing XHTML Web Page Files
PHP can write just about anything. The script below will concentrate on XHTML 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 used single quotes as well as 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 XHTML in a second. But first, let's check out the PHP aspects of the script. The $p variable is most of the script (15 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 XHTML file whose contents are what's in the PHP variable string $p.
According to Wikipedia, "Most major browser vendors were unwilling to implement the features in new W3C XHTML 2.0 drafts, and felt that they didn't serve the needs of modern web development. The Web Hypertext Application Technology Working Group eventually began working on a standard that supported both XML and non-XML serializations, HTML 5, in parallel to W3C standards such as XHTML 2. In 2007, the W3C's HTML working group voted to officially recognize HTML 5 and work on it as the next-generated HTML standard. In 2009, the W3C allowed the XHTML 2 Working Group's charter to expire, acknowledging that HTML 5 would be the sole next-generation HTML standard, including both XML and non-XML serializations. Of the two serializations, the W3C suggests that most authors use the HTML syntax, rather than the XHTML syntax." In other words, XHTML 1 and XHTML 2 are dead—long live HTML5 and XHTML5 (HTML5 plus XML, very roughly—it requires XML’s strict, well-formed syntax). (HTML5 should reach Candidate Recommendation stage during 2012 and be 100% ready for primetime by 2022, according to some sources.)
It's not as though the XHTML recommendation was wrong headed or black hearted. The idea was to get programmers to tighten up their coding as things like XML and others arrived on the scene, since the coding looseness had given browser makers headaches: "how fussy should we be about tag closing, and will our browser end up marginalized if we're loose and allow slop?" The biggest webmaster headache was that looseness allows various browser makers to implement differently, which led to webmasters wringing their hands and asking "OMG—which one will I write for?" The ugly reality is that, too often, the answer was: ALL OF THEM. This lead to object detection and browser sniffing and even separate pages for different browsers and platforms.
So you can see there is nothing wrong headed in a drive by W3C to tighten coding standards—making HTML more extensible and increasingly interoperable with other data formats. It's just that the browser makers were correct: XHTML (the way it was evolving toward a dead end) was not the answer. It truly didn't "serve the needs of modern web development." So HTML 5 to the rescue, right? It all depends on who adopts it and when and what they do to make us poor, downtrodden webmasters let go of our wicked ways, sloppy codes, and HTML sins. And whether they force it as opposed to asking nicely.
Having said that, why show how to write an XHTML page if even the W3C folks say use HTML, not XHTML ("do HTML5 coding rather than XHTML5")? For the same reason some people choose to climb mountains. Because they are there. Is there really any value to this stuff? Yes, to give you a laugh when you read the next paragraph! (Seriously . . . ) Note: In point of fact, hardly anyone uses XHTML code with an .xhtml extension. If you use a regular .html extension it will open in all browsers, and you can do as much excellent XHTML coding (with closed break tags, etc.) as you want and no one will care all that much.
Wait until you see the pants-wettingly funny thing that happens when you click the link below when you're in IE! Can you guess what will happen? We doubt it. What is the very very very very very very very very very very very very LAST thing you would expect to happen???????!!!!!!!! Well, guess what? THAT IS EXACTLY WHAT HAPPENS!!! Did you guess it yet? Well, here it is. When you click the link in IE, they frickin' send you over to their arch-rivals: FIREFOX!!!!!!!!!!!!!!!!!!!!!!!!!!!! Seriously, folks! It's January 2011 and that is what Internet Explorer 8 just did. (It is said that Microsoft has added support for true XHTML in IE9 as opposed to just the versions served as HTML. But this doesn't make their IE7 and IE8 joke on Firefox any less funny!) It's like they consider XHTML a Halloween bag of burning dog poop and they want the Firefox people to be the suckers that come running out onto their porches after the doorbell rings to stomp on the burning crap!!!!!!!!!! LOL LOL lol lol rotfl lmao bwl :D :D :D :D ROTFL!!!
So does it work on Firefox? Sure, if you make no errors. When Firefox needed to be burning the midnight oil remedying their pathetic support for video viewing, they were, instead, wasting it on XHTML. Microsoft knew better. Can't you just see ol' Bill Gates grabbing his groin as he says: "web standards? web standards? I got your web standards . . . RIGHT HERE!"? Ya gotta love it!!! This whole thing is rich! Just rich! What happens to would-be IE XHTML viewers that have no other browser installed? We haven't the slightest idea! (But don't be surprised if you see smoke pouring out of the back of your computer! LOL!!!)
Okay, since you're probably weary of exclamaion points and wisecracks, go ahead: "make my day" and "are you feelin' lucky?" (Dirty Harry). Go ahead—click the link below:
Here is the result: my_xhtml_file_written_with_php.xhtml
IMPORTANT NOTE: To see anything on an XHTML page, you must have IE9 or something non-IE. Or simply change the code below so it saves the page with an .html extension.
$p="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
<html xmlns=\"http://www.w3.org/1999/xhtml\"> \n
<title>my xhtml file written with php</title>\n
<p>This is the xhtml file written by a php page.<br />\n\n
How do you like it?<br />\n\n
Here is a link: <a href='index.html'>home</a>.
$a = fopen("my_xhtml_file_written_with_php.xhtml", 'w');