Apart from the challenge, there are actually times when it would be useful to check the contents of an FTP server without access to a PC.
Imagine you are responsible for checking that a number of clients send data into your system. You can't be at work all the time, and you can't always have access to a PC or laptop from which to dial into your internet account. But you almost certainly carry a mobile phone, and chances are that it's WAP-enabled (otherwise you wouldn't be reading this page!). If you have worked with computers for any length of time you almost certainly have your own network node in your local pub, bar, etc from which to discuss, er, work-related matters with your mates: now you can check your clients and harangue them for not sending data, from the comfort of your own bar-stool using your mobile phone. Best of all, when the itemised bill comes in you can claim it back from your boss who will undoubtedly be impressed that you were working so late.
Time for a short disclaimer..
.
The WAP pages described here make use of the FTP functionality built into later versions of PHP. The system I have built works
fine with my *nix hosted ISP - as always, your mileage may vary. This is strictly a proof-of-concept application: if you
don't like the security provided you should implement your own..
The goal in this version of the software was to be able to login to the FTP server as any specific user, and get a count of the number of files and directories - nothing more. Certainly not a file listing as this would easily break the WAP page size limit. Maybe this could be incorporated at a later stage if you really had to have this feature! Also remember it doesn't do directory recursion.
I wanted to make the system easy to use, with minimal "typing". If you have over 150 FTP clients as I do, you can probably remember their names, possibly their logins, but almost certainly not their passwords ! I therefore resolved to set up a small database on my hosted site in order to store a login name and a password. In this proof of concept model the password is not encrypted, but this should be relatively easy to implement. The usual cop-out statement here is ".. and this is left as an exercise for the reader". All I wanted to prove was that it could be done, so no flames about security, OK ?
The user has the option to search by part of a client (familiar) name or a login name. In the event of a multiple match you have the option to page through each hit and select the one you want. I have used my "test account" in ths example.
In order to provide some level of security, the password is rot13'd when it passes between pages.
Here is my dog-simple table. This holds the details of the FTP logins.
# Table structure for table 'FTP' held in database 'customer_database' # CREATE TABLE FTP ( Uname varchar(20) NOT NULL, Uid varchar(8) NOT NULL, Upw varchar(20) NOT NULL, UNIQUE Uid (Uid), KEY Uname (Uname, Uid) ); Example: Fred Bloggs | fb000000 | fredpass John Doe | jd000001 | jdpass
Once you have populated the table with data, you are ready to look at the first (main) page. Click
here
to launch this in a new window.
You will see that there's not much PHP in this page. The main point here is that you are entering your search type (name or login) and the
value you want to search for. Having done this, you can proceed with the actual search. Click
here
to launch this in a new window.
The main point about the search itself is how you do it on a mobile phone as opposed to a "real" PC. When I first started doing this type of development I followed (my) traditional practice of doing the search and retrieving all the results as they would then be displayed on a web page. On a mobile device though the conditions changed: you cannot guarantee that the "connection" will be reliably maintained, and I didn't want to leave my MySQL server with lots of disconnected links timing out. I therefore adopted the "hit and run" method shown below. This is essentially Connect / Query / Retrieve / Disconnect.
Here's how it is done:
mysql_connect("localhost", $DBUSER,$DBPASS);
mysql_select_db("customer_database");
$query = "SELECT * FROM usertable";
$query .= " WHERE $SearchType LIKE '%$SearchTerm%' LIMIT";
$query .= " $pointer,1"; // pointer is current record, incremented every time page is reloaded
$result = mysql_query($query);
$nr = mysql_num_rows($result);
In this scenario the first time that the page is loaded the record number pointer is set to '0', and PHP duly extracts just record 0. When the page is reloaded, the pointer is incremented by 1 and the next record (if any) is retrieved. I have found by experience that this way of running things works just fine for a mobile device and the time actually spent connected to MySQL is considerably reduced. Ok, you get lots of very short connections, but it works.
Ok, so now that you have a result you have the option to step through to the next one, or display your chosen record. This is where the third page is used to
perform the FTP part and you can click here
to launch this in a new window.
The page makes two attempts to persuade the WAP browser that it really needs to clear it's cache every time the page is loaded - if this doesn't happen you are liable to get incorrect results for file/directory totals. Some phones are better then others for this, and believe me, it's best to use all methods available to you!
I originally used PHP's ftp_nlist function but this did not give the desired results, and so I went for ftp_rawlist instead, and stored the resulting directory list in an array, called, $list. I understand from various forums that this may be broken under Windows, so check your results carefully.
The next step was to then examine each element of the array and if it was a directory or a file to update the appropriate total. This was done by examining the first chacter of each line:
while ( $list[$i] ) // .. loop round array..
{
if ( substr($list[$i], 0, 1 ) == "-" ) { $files++; } // look for real files (I.e. not symlinks)
elseif ( substr($list[$i], 0, 1 ) == "d" ) { $dirs++; } // look for directories
}
Here's one I prepared earlier: this only shows files, but if there had been a directory in there as well it would have started with a 'd'.
-rw-rw-rw- 1 0 0 178508 May 7 14:32 exblurb.txt -rw-rw-rw- 1 0 0 187916 May 7 14:32 exreview.txt -rw-r--r-- 1 0 0 742011 May 7 14:26 latest.txt -rw-r--r-- 1 0 0 1904 May 7 14:32 wapno1.rss
.. And finally, here are some screen shots showing the finished product.
| Main Screen | Doing a name search |
Found a match | Login and show result |
|---|---|---|---|
![]() |
![]() |
![]() |
![]() |
Pyweb.com Home of the wonderful Pyweb emulator
w3schools.com The source of much WAP knowledge
mysql.com A Great Database
php.net A Great Scripting Tool