图像缩略图&下载他们

时间:2013-03-28 06:28:22

标签: php html

我有以下代码用于在上传时自动创建数据库中图像的缩略图。

<?php
include('config.php');
$result = mysql_query("SELECT * FROM photos where caption='cars'");
while($row = mysql_fetch_array($result))
{
    echo '<div id="imagelist">';
    echo '<p><img src="'.$row['location'].'"></p>';
    echo '</div>';
}
?>

现在每当我点击图库中的图片时,我希望它在新标签页中打开图像并给我一个下载选项,我想要下载代码,它将从数据库中获取照片ID然后给我只下载该图像的选项。

1 个答案:

答案 0 :(得分:2)

试试这个:

    $file    = your file
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
在新版本的php中不推荐使用mysql_ *函数,所以使用mysqli_ *函数或PDO