gpt4 book ai didi

php - 下载 zip 文件中的照片

转载 作者:行者123 更新时间:2023-11-29 19:11:12 24 4
gpt4 key购买 nike

大家好,非常感谢您提供的任何帮助。在此代码中,使用表单将 img 上传到 mysql 数据库,这是数据库。

数据库

CREATE TABLE `user_pic` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`img` LONGBLOB NOT NULL,
`img_name` VARCHAR(200) NOT NULL,
`upload_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_id` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB

这里我将所有照片加载到变量中

load_img.php

$couple_login_id = $_SESSION['users']['id'];
$statement = $conexion->prepare("SELECT * FROM user_pic WHERE user_id = $couple_login_id");
$statement->execute();
$fotos = $statement->fetchAll();

这里我循环所有图像并将图像显示在html上

画廊.php

    <?php foreach($fotos as $foto):?>
<div class="col-xs-12 col-sm-4 col-md-3 item-photo">
<div class="photo">
<?php echo '<img class="img-responsive" src="data:image/jpeg;base64,'.base64_encode( $foto['img'] ).'"/>';?>
<?php echo '<a class="search zoom fancybox" href="data:image/jpeg;base64,'.base64_encode( $foto['img'] ).'"><span class="icon-search"></span></a>';?>
<?php if($_SESSION['users']["user_rol"] == 1):?>
<a class="star" href="#"><span class="icon-star"></span></a>
<?php endif;?>
<a class="download" href="#"><span class="icon-download"></span></a>
</div>
</div>
<?php endforeach;?>

这是下载 zip 文件中所有照片的代码。download_zip.php

  $zip = new ZipArchive();

# create a temp file & open it
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);

# loop through each file
foreach($fotos as $file){

# download file
$download_file = file_get_contents($file['img']);

#add it to the zip
$zip->addFile($download_file);

}

# close zip
$zip->close();

# send the file to the browser as a download
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=wedding_photos.zip");
header("Content-length: " . filesize('wedding_photos.zip'));
header("Pragma: no-cache");
header("Expires: 0");
readfile('wedding_photos.zip');

问题是,当我创建 zip 文件并下载它时,我可以打开它,因为 sed 该文件的格式未知或已损坏。如果代码有问题或者是因为照片存储在数据库中的方式有​​问题,我不知道。感谢您能给我的帮助。 :D

最佳答案

当您使用二进制数据而不是文件时,您应该使用 addFromString 方法。所以你可以将代码更改为:

$zip = new ZipArchive();

// create a temp file & open it
$tmp_file = tempnam('.', '');
$zip->open($tmp_file, ZipArchive::CREATE);

// loop through each file
foreach ($fotos as $file) {
//add it to the zip
$zip->addFromString($file['img_name'], $file['img']);
}

// close zip
$zip->close();

关于php - 下载 zip 文件中的照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43070714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com