DiigIT | IT Community
No Profile Image
Welcome Guest
New User? Register | Login

download the image from database

By: | 18 Feb 2009 4:25 pm

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

Comments

i put up a image in database and i want when a user click

looks as if you are storing the image file names in the database and not the
actual image data. This is the preferred method.

I take it that there is a distinction for you between displaying the image when
the link is clicked (a normal browser behavior) and downloading it to the
default download location on the client computer.

Convincing the browser to download rather than display is based on the MIME type
associated with the file. A image/jpeg value is normally processed by the
browser to be displayed in the browser. You could create a script which would
get the file and supply the appropriate MIME type that would normally trigger a
download on most browsers.

On many Linux systems, a file called /etc/mime.types provides a list of MIME
types used on that system. The Apache configuration files may also make special
handling rules for particular MIME types.

By: | 18 Feb 2009

Hi,

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: binaryn");
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.

By: | 18 Feb 2009

Leave a comment

Enter the text in the image
img
Can't read?
Type the characters you see in the picture below.


Close Move