Code Display Script

This page describes my code download folder, how it works, and what it does.

I wrote a file called show_save.php that reads a filename and then:

  1. Downloads the file to your computer if the request is something like /download/filename.ext/save/ and tells the browser to save it with the proper filename.
  2. Spits out the contents in plain-text if the textonly query-string var is set.
  3. Otherwise, spits out the contents using the Text_Highlighter PEAR class so that it’s easy to look at. (This allows for cleaner output and support for other languages, particularly HTML, CSS, and JavaScript.)

The last piece in the puzzle is the .htaccess rules to force any urls like /download/whatever get redirected to /download/show_save.php?file=whatever, and urls like /code/whatever to go to /download/show_save.php?file=whatever&textonly=1. Here they are:

[The requested file /home/yaohytmw/ could not be found]

With the help of mod_autoindex, the index page makes a nice listing of all the files available, and provides descriptions and links to the related blog post. Of course, the only drawback is that I have to manually add the descriptions in the .htaccess file each time I upload a new script. So, we’ll see how well that goes, or if I get lazy and start uploading stuff without any descriptions.

Here’s the .htaccess rules sitting in the download folder to set up the index:

[The requested file /home/yaohytmw/ could not be found]

It supports subdirectories now, as well as extensions other than .php and .txt.

Feel free to use these techniques on your own page if you’d like, or comment on any ideas that you think might make this better.

Leave a Reply

Comments are moderated like crazy using a variety of plugins. There is a very high likelihood that your comment won't show up right away, especially if you have never commented here before, but it was not deleted.

Please be patient, and do not post your comment more than once. It will show up once it is approved.

You must be logged in to post a comment.