CGI script to list files within a directory


9/8/02
 

CGI script to list files within a directory

Now for the next problem - what should the PHP script do actually? Well, it'll have to begin by finding out which HTML files exist - it has to be able to find the files before it can analyse them. While searching the web for information about invoking PHP scripts using crontab I happened to come across >>this tutorial page which gives the following as an example of a shell script:
#!/bin/sh
rm -f /tmp/listing.tmp> /dev/null 2>&1
touch /tmp/listing.tmp
ls -l [a-z]*.doc | sort> /tmp/listing.tmp
lpr -Ppostscript_1 /tmp/listing.tmp
rm -f /tmp/listing.tmp
 
This gave me the idea that I could perhaps use a similar script to produce a text file listing the files within a directory, ready for analysing by the Expiry Checker PHP script.

expirychecker.cgi

I tried this cgi script...
#!/bin/sh
#
# Test script just to create a list of files
#
# author = Martin Bush, South Bank University
# date = 9th August 2002
# version = 1.1
# history = -

# touch it first to create it if it doesn't exist
touch file-list.txt

# now pipe a list of the files within the directory to it
ls > file-list.txt

# for some reason it crashes without an echo like this...
echo Content-Type: text/plain
echo
echo The date and time at this server is `date`
# ...but I don't understand why!
 
...which worked. The URL required to invoking cgi scripts (on the SBU Unix system at least) is a little different from the URL for PHP scripts. In this case the cgi script is triggered by this URL:
http://www.sbu.ac.uk/cgi-bin/cgiwrap/~bushm/expirychecker.cgi
So far so good, although this isn't the whole story - because the Expiry Checker will have to analyse all the files not just in one directory but also in all the sub-directories of that directory. If the directory structure was fixed it would be probably be easier, but given that we'll sometimes need to create new sub-directories or delete old ones I really need to create a script that is able to find all the sub-directories, and then the files within them, by itself. Right now I can't think of a way to do that!
 
 

<<contents ^top^ next>>