使用PHP下载多个图像

时间:2016-07-20 12:41:10

标签: php image-processing

查询返回71行,但只下载了一个图像。所有图像文件都是jpegs,最大文件大小为2 MB;大多数都不到1 MB。 fsize行被注释掉,因为它们无法返回jpg图像文件。

我打开下载的一张图片没有问题,所以我正在做正确的事情。我如何获得其他70张图片?我没有深入PHP,但我完成了我需要完成的工作。这是我的代码:

    <?php

    $query = "SELECT imagefilename FROM imageinfo WHERE accepted = 1 ";
    $result = mysqli_query($connect, $query);
    while ($row = mysqli_fetch_array($result))
    {
        $imagefilename = $row[0];

        $sourcefile = 'http://domain.com/images/'.$imagefilename;

        if ($fd = fopen ($fullPath, "r"))
        {
                //$fsize = filesize($sourcefile);

                // if IE, otherwise Content-Disposition ignored
                if(ini_get('zlib.output_compression'))
                    ini_set('zlib.output_compression', 'Off');

                header("Content-type: image/jpg");
                header("Content-Disposition: attachment; filename=$imagefilename");
                //header("Content-length: $fsize");
                header("Cache-control: private");
                while(!feof($fd))
                {
                    $buffer = fread($fd, 2097152);
                    echo $buffer;
                }
        }
        fclose ($fd);
    } //end while
    ?>

1 个答案:

答案 0 :(得分:0)

将几个帖子拼凑在一起,这是我结束的工作代码。                 

    //This puts the selected images in a zip file.

    ignore_user_abort(true);
    set_time_limit(0); // disable the time limit for this script

$query = "SELECT imagefilename FROM imagetable WHERE accepted = 1 ";
    $result = mysqli_query($connect, $query);
    if (mysqli_num_rows($result) == 0)
    {
        //Send message.
    }

    // common vars
    $file_path = 'path_to_image_file';

    $zipname = 'images.zip';
    $zip = new ZipArchive;
    $zip->open($zipname, ZipARCHIVE::CREATE );
    while ($row = mysqli_fetch_array($result))
    {
        $filename = $row[0];
        $zip->addFile($file_path.$filename,$filename);
    }
    $zip->close();

    if (!file_exists($zipname))
    {
        //Send message
    }
    //This downloads the zip file.
    //zip headers
    if (headers_sent())
    {
        echo 'HTTP header already sent';
    }
    else
    {
        if (!is_readable($zipname))
        {
            header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
            echo 'File not readable';
        }
        else
        {
            header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
            header("Content-Type: application/zip");
            header("Content-Transfer-Encoding: Binary");
            header("Content-Length: ".filesize($zipname));
            header("Content-Disposition: attachment; filename=\"".basename($zipname)."\"");
            header("Pragma: no-cache"); 
            header("Expires: 0"); 
            readfile($zipname);
            exit;
        }
    }   

    ?>