Write MPEG Video Website Page Files with PHP
PHP can write just about anything. The script below will concentrate on MPEG video 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 quotes in the $p string with backslashes. That's safer than trying to convert them to single quotes and ending up with an error because we missed a contraction somewhere. And of course, the newline characters are escaped too (\n). In the HTML, we centered the video on the screen. Then we used the classic embed method of putting an embed tag inside an object tag. The height is bigger than the video to make room for player controls. The clip is just a short sample from The Squirrel Valley Railroad but you can change it to your own MPEG video—don't forget to change the height and width. Give the height 46 more than the video size. The video is set to autostart, but you may alter the player parameters any way you want. You may need a plugin installed on Firefox. We installed a couple on Firefox. The Windows Media Player and Quicktime plugins are freely available.
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.
Here is the result: my_mpeg_video_webpage_file_written_with_php.html
<title>My mpeg video webpage file written with PHP</title>\n
<center><div>My mpeg video webpage file written by a PHP page. How do you like it?<BR>\n
<object id=\"MediaPlayer1\" CLASSID=\"CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95\" codebase=\"http://activex.microsoft.com/activex/controls/mplayer/en/
standby=\"Loading Microsoft Windows® Media Player components...\" type=\"application/x-oleobject\" width=\"384\" height=\"338\">
<param name=\"fileName\" value=\"test-mpeg1.mpg\">
<param name=\"animationatStart\" value=\"true\">
<param name=\"transparentatStart\" value=\"true\">
<param name=\"autoStart\" value=\"true\">
<param name=\"showControls\" value=\"true\">
<param name=\"Volume\" value=\"-450\">
<embed type=\"application/x-mplayer2\" pluginspage=\"http://www.microsoft.com/Windows/MediaPlayer/\" src=\"test-mpeg1.mpg\" name=\"MediaPlayer1\" width=384 height=338 autostart=1 showcontrols=1 volume=-450>
$a = fopen("my_mpeg_video_webpage_file_written_with_php.html", 'w');