Jump to content

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.';
}
?>

Edited by 3raser

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.