Automate tasks and improve ease of use when processing data on the web
Currently indisposed
It’s quite straightforward to do this via a couple of headers in your script;
I’ll look at how do to it with PHP, since that’s the language of choice round
here, but you can also do it in others, including Perl with CGI.pm.
The trick is to define the content type in the usual way – for example, text/plain, and then to add an extra Content-Disposition header, which tells the receiving browser that it’s an attachment and what name to use to save it.
In the first code example, we’re going through a list of timers saved from a PVR as a backup; the intention is to use the script to update all the channel numbers to take into account a reshuffle and then reimport the list of timers, so all our recordings carry on fine.
The following code specifies a filename:
$timerdata = file($_FILES[‘timerfile’] [‘tmp_name’]) ;
header(‘Content-Type: text/plain’) ;
header(‘Content-Disposition: attachment;
filename=TimerList.txt’) ;
foreach( $timerdata as $timer ) { // process a line of data }
exit ;
As you can see, it’s very straightforward; just one header to output, with the filename specified, and it works fine when you’re running everything through a web browser (see screen 2). But that’s not always the case.
That may sound like a strange thing to say of a web application, but web browsers aren’t necessarily the only thing that will look at your site.
In the example we just gave, it will most likely be a browser, since the user will be using it to select a file to upload to the script initially; but in another case, it might not be.
If information is designed to be retrieved unattended – for example, a script that’s fetching data from a central database – you may need to code things a little more carefully.
A custom script that queries a database on your site can save the results however it likes; that’s not really a problem, it’s up to the script’s author. But what if your database is intended to produce a file for people to use?
Let’s say you have a script called fetchdata.php, which lets people request a file, called categoryinfo.txt, via a URL that ends with something like fetchdata.php?catid=21.
That’s going to work fine with the PHP code we have above as long as people use a web browser.
But suppose someone wants to automate it, and have the file ready first thing each morning.
One of the simplest tools to fetch parts of a website is wget ; Usually, you give it a web address and it downloads the file to disk; it’s great for copying a website, fetching downloads, and is often used in scripts to fetch data.
Related articles
Q.Why are some of the keys on my keyboard doing strange...
Q.Is my phone’s Bluetooth any use?
Q.Can I switch boot drives so that I can work on older...
Old Street roundabout is being touted by the Government as the UK's answer to Silicon Valley, but it seems our best innovations are coming from all over the UK
|
|
|
|
|
Computeractive Excel (2010) Online tutorialPrice: £19.99 |
Computeractive Word (2010) Online TutorialPrice: £19.99 |
Computeractive Powerpoint (2010) Online TutorialPrice: £19.99 |
Angry BirdsPrice: £9.99 |
Back Issue CD-Rom 14 (2011)Price: £15.99 |