Jump to content

Button To Download Images


darphas

Recommended Posts

$path should be the path to the image you want the viewer to download.

 


<?php
//path to the picture you're wanting the viewer to download
$path = 'picture.gif';

if(file_exists($path)){
//split the extension and name from eachother
$e = explode('.', $path);

//get the name of the file
$file_name = $e[0]; 

//image extension
$extension = $e[1];

// Send a header saying we'll be displaying a picture
header('Content-type: image/'. $extension);

// Name the file
header('Content-Disposition: attachment; filename="'. $file_name .'.'. $extension .'"');

// Path to the picture you're wanting the user/viewer to download
readfile($file_name .'.'. $extension);
}else{
echo 'The requested image does not exist.';
}
?>

Well, you can make a small modification to the $path variable:

 


$path = trim($_GET['file']);

 

Then, you'd simply link to it:

 

<a href="download.php?file=picture.gif">This is a link. Download the image.</a>

 

And of course download.php would be changed to whatever you named the PHP file I provided to you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.