Jump to content

download image from homepage

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Bertan

Bertan

    Learning Programmer

  • Members
  • PipPipPip
  • 43 posts
Not sure if I'm writing in the correct forum or if this can be done with simple html.

Anyway, I want to make a link with the text "download image", if someone clicks it same window opens that opens when you click "save image as".

Any help on this one?

#2
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
This should do it :

<?php


$filename = $_GET['file'];


// required for IE, otherwise Content-disposition is ignored

if(ini_get('zlib.output_compression'))

  ini_set('zlib.output_compression', 'Off');


// addition by Jorg Weske

$file_extension = strtolower(substr(strrchr($filename,"."),1));


if( $filename == "" ) 

{

  echo "<html><title>eLouai's Download Script</title><body>ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath</body></html>";

  exit;

} elseif ( ! file_exists( $filename ) ) 

{

  echo "<html><title>eLouai's Download Script</title><body>ERROR: File not found. USE force-download.php?file=filepath</body></html>";

  exit;

};

switch( $file_extension )

{

  case "pdf": $ctype="application/pdf"; break;

  case "exe": $ctype="application/octet-stream"; break;

  case "zip": $ctype="application/zip"; break;

  case "doc": $ctype="application/msword"; break;

  case "xls": $ctype="application/vnd.ms-excel"; break;

  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;

  case "gif": $ctype="image/gif"; break;

  case "png": $ctype="image/png"; break;

  case "jpeg":

  case "jpg": $ctype="image/jpg"; break;

  default: $ctype="application/force-download";

}

header("Pragma: public"); // required

header("Expires: 0");

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header("Cache-Control: private",false); // required for certain browsers 

header("Content-Type: $ctype");

// change, added quotes to allow spaces in filenames, by Rajkumar Singh

header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );

header("Content-Transfer-Encoding: binary");

header("Content-Length: ".filesize($filename));

readfile("$filename");

exit();


?>


Just hyperlink to the php page and it will download it for you.

Source : eLouai's Force Download of media files script

:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it

#3
Bertan

Bertan

    Learning Programmer

  • Members
  • PipPipPip
  • 43 posts
I have this script in the root folder and it works great for images places in the same folder.
Is it possible to make this script work for other images as well without changing the position of the script.

It works fine for this image also placed in the root folder:
www.site.com/force-download.php?file=image.gif

But I want it to work for this image too:
www.site.com/gallery/image.gif