PHP Script for Writing ASP Web Page Files
PHP can write just about anything. The script below will concentrate on ASP 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 inside the $p string (in the response.write function) since 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 ASP in a second. But first, let's check out the PHP aspects of the script. The $p variable is most of the script (12 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.
What about classic ASP, which uses the .asp extention? Given how widespread classic ASP is, we believe Microsoft will support it for a long time to come, but only marginally, while pushing ASP.NET hard. If you do not already have Microsoft programming experience, ASP is probably not a good choice. PHP is easier in so many ways. However, ASP is a server side programming language based on Basic. Basic is a beginners programming language. So if you know Basic, there's not much of a learning curve to learn ASP. A good Windows host will allow you to run ASP, ASP.net and PHP with MySQL or SQL server all on the one site. But Windows hosting plans cost more than Apache/Linux plans.
In ASP, an interpreter like VBScript is used to execute the server-side code. Here is the sequence: The browser sends a request for the ASP page to the Windows IIS server. The web server receives the request and sees the extension '.asp' so it retrieves the proper ASP file from disk or memory. The web server sends this ASP file to a special program called 'ASP.dll'. This ASP file is processed from top to bottom and any encountered ASP commands are executed. The result of this process is a standard HTML file, which is sent back to the browser. The efficiency of page rendering is affected by this setup in several ways. A common optimization method for ASP applications is to move some of the server-side script into pre-compiled COM components to enhance response time. And the mixing of server-side evaluation blocks with static HTML is another efficiency concern, since it's less efficient than evaluating a single server-side block because the interpreter has to be invoked repeatedly. Another inefficiency creeps in when PHP is on a Windows server and the programmer has to learn the various exceptions incurred by not being on Apache. If you look through enough PHP functions on php.net you will see what we mean. Anything requiring extra time is more expensive since time is money.
As a fix for the ASP inefficiencies, many ASP developers resort to large blocks of server-side script to replace static HTML elements with Response.Write() functions instead. ASP permits the inclusion of different blocks of scripts in a page to be written in different script languages. This is good in ways because the ASP page can therefore be more dynamic, but it also reduces performance by requiring that a particular page load both scripting engines to process a request that consumes more time and memory than just using one language.
But in ASP.NET, the pages are always compiled into .Net classes housed within assemblies. This class includes both the server-side code and static HTML, so when the page is accessed for the first time, subsequent rendering of that page is serviced by executing the compiled code. All the inefficiencies of the scripting model of traditional ASP are eliminated by this.
With ASP, or ASP.NET, you need the necessary licenses (which non-Windows servers do not) so either is more expensive than Apache/Linux/Unix servers.
The ASP.NET web application framework, using the .aspx extension, originates from Microsoft. Is it used much? Not compared to PHP. And big players don't use regular ASP very much. But is ASP.NET used by the big players? Facebook, Youtube (uses Python too), Yahoo, Wikipedia, WordPress and Baidu are written in PHP. Live and MSN and MySpace are ASP.NET websites. Google uses Python and C++ with lots of Ajax for page display. Twitter uses Ruby on Rails and Scala. Amazon.com, bbc.co.uk, Priceline.com, and Craigslist use a lot of Perl. Yahoo benchmarked their own compiled custom-C language against PHP+eAccelerator and Perl+FastCGI, and found that the difference wasn't very large. So they chose PHP as their primary development language. Java is used in Google's Gmail. Oracle (Java's owner) has a section on their site entitled Why Businesses Are Choosing Java, yet not a single example of such a business is given, but we found that Dell, NewEgg, and Buy.com use it.
Yet Microsoft insists that: "Develop, deploy and easily manage Web applications using your choice of languages. From ASP.NET to PHP, IIS7 provides a powerful and flexible Web server environment for the world’s most popular Web applications. IIS7 provides a single Web server platform for developing, deploying, hosting and managing the most popular languages used on the Web, from ASP.NET to PHP." One gets the idea that they are seeing which way the wind is blowing and wish to get in the flow, so they emphasizing their servers can run PHP even while hawking ASP.NET. Notice that Apache servers do NOT talk about running ASP classic (.asp) or ASP.NET (.aspx). Only Windows servers can run ASP installations.
One other thing: The PHP imagegrabwindow() and imagegrabscreen() functions are only available on Windows servers. If you need/want to do a lot of screen or window capturing work because you need to take snapshots of web pages for whatever reason (such as creating thumbnail images for some application or client), these PHP functions and a Windows server are really the only way to go. We'd sure love to see Apache allowing these functions, but they probably feel they are too server intensive, although they reportedly grab screens in only 2 or 3 seconds so perhaps it's only a long series of such function executions they're concerned about.
Here is the result: my_asp_file_written_with_php.asp
IMPORTANT NOTE: To see anything on an ASP page, your server must be running the Microsoft IIS (Internet Information Server) operating system, version 3.0 or higher. If not, you will not be seeing what the ASP page contains, no matter what.
<title>my asp file written with php</title>\n
<% response.write('<h2>ASP page test</h2><P>To see anything on an ASP page, your server must be running the Microsoft IIS (Internet Information Server) operating system, version 3.0 or higher. If not, you will not be seeing what the ASP page contains, no matter what.</P>') %>\n
$a = fopen("my_asp_file_written_with_php.asp", 'w');