将图像文件添加到存档

Adding Image Files To Archive

您好,我在将文件添加到存档时遇到了这个问题,如果有人可以帮助我,这真的对我有帮助。基本上我需要添加到存档中的所有文件都在一个 csv 文件中。我正在使用 fgetcsv 读取此文件,然后尝试遍历每个图像并将其添加到存档中。

        $media_file = "C:/e_files/Exports/Imagery.csv";

        $zip = new ZipArchive();
        $zip->open('C:/e_files/Exports/Feeds/Media.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);


        if (($handle = fopen($media_file, "r")) !== FALSE) 
        {
            $row = 1;

            while (($data = fgetcsv($handle)) !== FALSE) 
            {
                if($row > 1)
                {
                    $image_name = isset($data[1]) ? $data[1] : null;

                    if(!empty($image_name))
                    {
                        $zip->addFile("C:/e_files/Images/$image_name");
                    }
                }

                $row++;
            }
        }

        $zip->close();

$media_file 是包含所有需要添加到存档的图像名称的文件。

C:/e_files/Images 是存储所有图像的路径,我必须从中获取特定图像并添加到存档中。

但是当我 运行 这样做时,它会将整个 C:/e_files/ 添加到 zip。

使用第二个参数localname:

$zip->addFile("C:/e_files/Images/$image_name", $image_name);

localname
If supplied, this is the local name inside the ZIP archive that will override the filename.

来源:http://php.net/manual/en/ziparchive.addfile.php

文件名似乎从路径派生不正确,并且始终是相同的值,它不断覆盖以前添加的文件。