【问题标题】:Storing multiple downloaded images from database - PHP从数据库存储多个下载的图像 - PHP
【发布时间】:2013-04-11 00:54:19
【问题描述】:

我在数据库中将各种图像拆分为 blob 文件。我编写了一个 php 脚本来访问这些 blob 文件并将它们下载到本地。

由于有很多图像,我放置了一个循环来下载存储在数据库中的所有图像,但是当我在循环中运行脚本时,我得到一个大的下载并且没有单独的图像。

我的代码如下:

$query="SELECT `id` FROM `images` LIMIT 10";
$result=mysql_query($query);
$imageid = array();

while($row = mysql_fetch_assoc($result)){
//iterate over all the fields
foreach($row as $key){ 
    $imageid[] = $key;
    }
} 

$fileName ='';

for ($i = 0; $i < count($imageid); $i++) {

    $query="SELECT `filename` FROM `images` WHERE `id`=".$imageid[$i]."";

    $result=mysql_query($query);

    while($row = mysql_fetch_assoc($result)){
        foreach($row as $key){ 
            $fileName = $key;
        }
    } 


    header("Pragma: public");
    header("Expires: 0"); // set expiration time
    header("Cache-Control: must-revalidate, post-check=0,pre-check=0");

    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");

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

    header('Content-type: application/octet-stream');
        header('Content-Disposition: inline; filename='.$fileName.'');

    $sql = "SELECT `data` FROM `images_data` WHERE `image_id`=".$imageid[$i]." ORDER BY id";    
        $result1=mysql_query($sql);

    while($row = mysql_fetch_array($result1)) echo $row['data'];




}//END FOR LOOP

有什么方法可以通过运行脚本下载所有图像? 如果我手动更改 $imageid[$i] 的参数(例如 0,1,2,3 等),将下载不同的图像,但对于我必须使用的图像数量来说它是无用的。

非常感谢任何帮助!

【问题讨论】:

  • 只是想发表评论。请不要将图像作为 blob 存储在数据库中,这会使数据库变得异常臃肿,数据库服务器难以有效使用,将图像存储为文件并存储它们数据库中的文件名
  • +1 到@bizzehdee,任何 sql 数据库都不适合这种用途。

标签: php sql file download


【解决方案1】:

您只能从一个 php 脚本运行中获取一次文件。 在您的情况下,我认为最好的方法是,将 DB 中的所有文件存储到临时文件夹中,当 zip(或 GZ)此文件夹时,并在您的 header() 中使用相应的 MIME 类型输出此 ZIP(或 GZ)存档后

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 2017-07-22
    • 1970-01-01
    • 2022-11-14
    • 2016-12-28
    • 1970-01-01
    • 2017-12-12
    相关资源
    最近更新 更多