Simple clear advice in plain English

How to use Perl or PHP scripts on your web site

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.

Reader Comments

   

Add your comment

All fields must be completed. Your email address will not be displayed or used to send marketing messages.

All messages will be checked by moderators before appearing on the site.

See our Privacy Policy for more information.

Related articles

Security shield illustration

How to use the Command Prompt

Lurking under Windows is the little-known world of the command line. We explain how to use this to fix faults and make your PC much more secure

Onscreen keyboard screenshot

Can I fix the Backslash key on my keyboard or do I need a new one?

There are several things to do to try and fix this problem: remove the cap and clean underneath; buy a new keyboard; or finally, use the on-screen version

Apple Mac OS X Lion desktop on a MacBook Air

Apple Mac OSX Lion operating system

A simple-to-use operating system with loads of features

Question & Answer

Q.Why are some of the keys on my keyboard doing strange...

> Read the answer

Q.Is my phone’s Bluetooth any use?

> Read the answer

Q.Can I switch boot drives so that I can work on older...

> Read the answer

Best deals on the web

img

Samsung RV520-A07

£359.98- Buy it now

img

Acer Aspire 5750G (LX.RXP02.019)

£399.99- Buy it now

img

Apple MacBook Pro (MD313B/A)

£904.37- Buy it now

Latest issue & subscription deals

Poll

Are you concerned about viruses that target mobile phones?

Jargon Buster

Computing terms explained in plain English

Restore point

A Windows backup of system files and settings.

Great shopping deals from Computeractive