I have the following

while($file = readdir($dp)){
		if($file != '.' && $file != '..')
			echo "<TD>";
			echo "<a href='$image/$file' target='_blank'><img src='$image/$file' width='120' height='120' alt='image'><br>$file</a>\n";	

			echo "</TD>";

			if($i == 5)
				echo "<TR>";
				$i = 0;

well that is the juist of it but anyway all i want is to be able to have a link that is called download and when you click on it the files donwloads not views it, did see this somewhere but not sure where.

Can anyone help?

11 Years
Discussion Span
Last Post by Troy

The "problem" is of course that your browser "understands" certain file types, so if you navigate to an image file such as .gif, .jpg, .png, it will display the image because it knows how to do that. Likewise, if you have PDF extensions installed, when you navigate to a PDF file, it will display in the browser instead of download. However, if you click on an unknown file extension, it will just prompt to download.

So how do you prompt to download recognized file types? One way is to just tell your users, "Right-click and choose Save As". Of course, that is not as fool-proof as you'd like. :)

The other option is to use a scripting language to stream the file to the browser. Below I'm going to post an example to serve an image (could be gif or jpg). This technique can be used to serve PDF, Excel, Word docs, etc., but you have to figure out the correct Content-Type for each.

$data = file_get_contents("img/my_image.jpg");
header("Content-Type: image/jpeg"); 
header("Content-Disposition: attachment; filename=my_image.jpg"); 
echo $data;

I use this technique in my class_http to facilitate image streaming. I also use this technique to force download of dynamically generated vCards in my class_vcard.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.