A while ago I promised 2 little scripts. Well, one of them is the source of the index file i use for the cardamar mirror (Source Code). Since I wanted a bit of writing to go with it, i’ll just put it in here:
the script is reletively simple. the top is just the top part of the html and as such can be set up as you desire. inside the php, we first define an array of files we do not want to list. every directory has ‘.’ and ‘..’ meaning current folder and up one level respectively and probably should be ignored. .htaccess is the standard Apache directory settings file and also should be ignored. you also should make sure that you don’t include the file itself and any other files you don’t want to list in the array (in this case, i also exclude source.php). it then opens the current directory (‘./’) and starts looping through all of the files. if it finds it in that array or the file ends with .md5 or .diz, it does not display the file (continue forces the loop into another iteration). if it does find the file, it calculates the filesize in megs (rounded to the nearest tenth). $md5_s is the string that contains all of the md5 information if available. it trys to find the md5 of the file in a file with the exact same name as the file with .md5 at the end. the file contains only the md5 hash. if found, it makes a nicer looking bit of html which will be included. if no md5 can be found, $md5_s is still blank when it is included. the description of the file is found in a file of the same name as the original file with .diz at the end. It then echos all the information in html with heredoc syntax.
Updates: You could easily integrate as much as you wanted of this into a database. I chose not to since there aren’t many files that I am going to need to handle and physical files allows for people to make direct requests to the files without doing any tricks (like setting up a .htaccess file that passes the file name as the parameter and could then query the database for it. somewhat of a demo later). You could still keep the files physical and the md5 and/or descriptions in a database as well, but that would be best suited for large amounts of files (of which you also might want to put pages in as well).
Remember that source.php file that I excluded and that demo i mentioned? well, you can take a look at the source of source.php at http://cardamar.quad341.com/source.phps. funny, that looks like a script that is displaying the source of files, but the files with the parameter of ‘f’ in the get parameters (also note that i deny access to any file that begins with a . since they are supposed to be either hidden or are in another directory except for .htaccess so i can use this to display it). Well, we haven’t been going to http://cardamar.quad341.com/source.php?f=index.php or http://cardamar.quad341.com/source.php?f=source.php to view the source of the files but it does seem that this is what is parsing the files… so what gives? This is where .htaccess redirects comes into play (and why i allow people to see my .htaccess). If we go over to http://cardamar.quad341.com/source.php?f=.htaccess and take a look, you can see a simple example of using mod_rewrite to make the redirection of what seems to be a physical file to actually feed into another file. You can see that it enables the RewriteEngine and then searches for a REQUEST_URI (that’s the part after the domain) that begins with / and ends with .phps and selects everything in between. If it finds this, it feeds it to source.php?f= with after that filled in with the file name with .php on the end. Fun, eh?
Well, that’s one (well…three, but i’m still giving out one more). This other one is just a function. I set it up because someone needed to be able to sort some dates that were formed a specific way. I’m not sure how many people really need to sort dates after they have been put into visual form, but the function will. It searches for the month location and the year location (year is optional though) and then sorts it in order. You can also look at this for an example of using array_multisort. You can see the source of the function here and the output here.
Now for why I’m a moron:
I was waiting for a special person to get online, so i decided i’d message the aol server until they came on. This actually took a while and i had a lot of anger to take out on aol. suddenly the server stops sending me errors. ok, so i guess they signed in. i have since found out that the server just blocked me. it no longer took my messages. this caused some misunderstandings later… but needless to say, i just hate aol more now.