----- Original Message ----- 
From: "zarilahr"

Dear Sirs

i put up a image in database and i want when a user click on
download link the particular image start to download. I have stored
100 images in mysql and below is the sample mysql query i write and
its work fine .

INSERT INTO `mobile`.`stuff` (`prod_id`, `cat_id`, `image_name`,
`image_path`)
VALUES (NULL, '3', 'Impossible ', 'impossible.jpg');

"please note that prod_id is auto increment."

and here is the php code how i called my all images with the help of
while loop from mysql.

echo '<td>' . '<img src="images/' . $row['image_path'] . '"
border="0">' .
'<br>' . $row['image_name'] . '</a> <br />' . '<a href="images/?
id ">  Download  </a>' . '</td>';

i want to start download the particular image when i click on the
Download link. Please tell me the code how i can download the image
when i click on Download link.

A screent shot with the name of imagedownlod is attached which can
clarify what exactly i want.

regards
Azhar
------------------------------------

NOTE: I am a php newbie - so please anyone that sees a mistake or security 
problem then please say so.

Hi Azhar,
             It looks like you already have a script for the image 
download -

'<a href="images/?id ">  Download  </a>'

Anyway - place the following at the absolute beginning of the page that 
displays the images.

<?php

if(isset($_GET['img']))
  {
  if(download_img($_GET['img']))
    {
    die();
    }
  }

function download_img($image_filename)
  {
  $path_parts = pathinfo($image_filename);
  $dirname = $path_parts['dirname'];
  if(($dirname != "") && ($dirname != ".")) return FALSE;
  $ext = $path_parts['extension'];
  $permitted = "jpg jpeg png gif bmp";
  $permitted = explode(" ", $permitted);
  foreach($permitted as $this_ext)
    {
    if($ext = $this_ext)
      {
      return output_img($path_parts['basename']);
      }
    }
  return FALSE;
  }

function output_img($filename)
  {
  if(!file_exists("images/" . $filename))
    {
    //echo("[" . $filename . "] Does not exist<br>\n");
    return FALSE;
    }
  if(!is_readable("images/" . $filename))
    {
    //echo("[" . $filename . "] Is not readable<br>\n");
    return FALSE;
    }
  header("Cache-Control: public, must-revalidate");
  header("Pragma: hack");
  header("Content-Type: application/octet-stream");
  header("Content-Length: " . filesize($filename));
  header('Content-Disposition: attachment; filename="' . $filename . '"');
  header("Content-Transfer-Encoding: binary\n");
  readfile("images/" . $filename);
  return TRUE;
  }

?>

----------------------------------
'<a href="images/?id ">
----------------------------------
and then change the above html to -
-----------------------------
'<a href="' . $_SERVER['PHP_SELF'] . '?img=' . $row['image_name'] . '">'
-----------------------------


Notes:
URL no longer contains path information for security. All images must be in 
a subfolder (images/) from where the script is running.
To change this to an absolute path then in output_img change the three 
"images/" to "/new_path/"

Thanks, Robert.


Reply via email to