简体   繁体   English

尝试创建一个zip文件以下载多张图片

[英]Trying to create a zip file to download multiple pictures

I've been playing with the following script for the last hour now and can not figure out what the hell is going wrong. 我最近一个小时一直在使用以下脚本,无法弄清楚到底出了什么问题。

What's the idea:- Basically I have a database that holds data on images and I am calling that data (image filenames), trying to put it into an array to then pass to a zip creation script. 有什么想法:-基本上,我有一个数据库,其中保存有图像数据,并且正在调用该数据(图像文件名),尝试将其放入数组中,然后传递给zip创建脚本。

Here's what I have:- 这是我所拥有的:

$query_ImageCollect = "SELECT * FROM image_data WHERE image_dealerid='$dealer_id' AND imagename LIKE '$imageName'";
$ImageCollect = mysql_query($query_ImageCollect, $vwconn) or die(mysql_error());
$row_ImageCollect = mysql_fetch_assoc($ImageCollect);
$totalRows_ImageCollect = mysql_num_rows($ImageCollect);
$counter=0;
do{
    $counter=$counter+1;
    $ImageUrl=$row_ImageCollect['image_url'];
    $getFileNames="../../images/$ImageUrl";
    if($counter==1){
    $getFiles="'$getFileNames'";
    }else{
    $getFiles="$getFiles, '$getFileNames'";
    }
    } while ($row_ImageCollect = mysql_fetch_assoc($ImageCollect));
    $groupFiles=array("$getFiles");
    $zipName="Pictures.zip";
    $zip = new ZipArchive;
    $zip->open($zipName, ZipArchive::CREATE);
        foreach ($groupFilesas $file) {
            $zip->addFile($file);
        }

    $zip->close();
    header('Content-Type: application/zip');
    header('Content-disposition: attachment; filename='.$zipName);
    header('Content-Length: ' . filesize($zipName));
    readfile($zipName);

I also tried parsing the results directly into an array but it also failed:- 我也尝试将结果直接解析为数组,但也失败了:-

$query_ImageCollect = ("SELECT image_url FROM image_data WHERE image_dealerid='$dealer_id' AND imagename LIKE '$imageName'") or die(mysql_error());

while( $row = mysql_fetch_assoc( $query_ImageCollect )){
$groupFiles[] = $row;
}
$zipName="Pictures.zip";
$zip = new ZipArchive;
$zip->open($zipName, ZipArchive::CREATE);
    foreach ($groupFilesas $file) {
        $zip->addFile($file);
    }
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipName);
header('Content-Length: ' . filesize($zipName));
readfile($zipName);

Any help / suggestions would be much appreciated. 任何帮助/建议将不胜感激。 Thanks in advance :) 提前致谢 :)

You are not actually adding anything to the zip as you are not processing the query result correctly. 您实际上并未在zip中添加任何内容,因为您未正确处理查询结果。 You are trying to pass a $row array as the filename to $zip->addFile() 您试图将$ row数组作为文件名传递给$zip->addFile()

You also dont need to create a temporary array from the query result and then process that you can do it all as part of the first query result processing loop. 您也不需要根据查询结果创建一个临时数组,然后将其作为第一个查询结果处理循环的一部分进行处理。

Also in your second attempt you are not issuing the query for execution, so there will be no results to process. 同样,在第二次尝试中,您不会发出要执行的查询,因此将没有结果可处理。

Try this instead :- 尝试以下方法:-

$query_ImageCollect = "SELECT image_url 
                       FROM image_data 
                       WHERE image_dealerid='$dealer_id' 
                         AND imagename LIKE '$imageName'";

$ImageCollect = mysql_query($query_ImageCollect, $vwconn) or die(mysql_error());

$zipName="Pictures.zip";
$zip = new ZipArchive;
$zip->open($zipName, ZipArchive::CREATE); 
while( $row = mysql_fetch_assoc( $ImageCollect)) {

    // make sure you dont need to add anything to the 
    // $row['image_url'] to make a correctly pathed filename

    $zip->addFile($row['image_url']);
}

$zip->close();

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipName);
header('Content-Length: ' . filesize($zipName));
readfile($zipName);

If you truely got no errors reported anywhere, then something is badly wrong with your config. 如果您确实没有在任何地方报告错误,则说明您的配置存在严重错误。 The php error file should be HUGH. php错误文件应为HUGH。

Also please dont use the mysql_ database extension it has been deprecated for years now, especially if this is a new development. 另外,请不要使用已被弃用多年的mysql_数据库扩展名,特别是如果这是新开发的话。 See this for help moving to mysqli_ or PDO 请参阅此以获取转向mysqli_PDO 帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM