在 PHP 中使用 popen 删除 zip 内的路径?

Remove path inside zip using popen in PHP?

我正在使用下面的脚本 post LAMP: How to create .Zip of large files for the user on the fly, without disk/CPU thrashing

header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename="file.zip"');

// use popen to execute a unix command pipeline
// and grab the stdout as a php stream
// (you can use proc_open instead if you need to
// control the input of the pipeline too)
//

$fp = popen('zip -r - tmp/01-55-34_561764e6cb06e', 'r');

// pick a bufsize that makes you happy (8192 has been suggested).
$bufsize = 8192;
$buff = '';
while( !feof($fp) ) {
   $buff = fread($fp, $bufsize);
   echo $buff;
}
pclose($fp);

我的文件在文件夹中:tmp/01-55-34_561764e6cb06e

我得到了一个可用的 zip 文件,但问题是我也得到了包含在我的 zip 文件中的路径。

我的 zip 文件打开时看起来像这样

tmp/01-55-34_561764e6cb06e/image1.jpg
tmp/01-55-34_561764e6cb06e/image2.jpg
tmp/01-55-34_561764e6cb06e/image3.jpg
tmp/01-55-34_561764e6cb06e/image4.jpg

我只想要图像,没有文件夹 (tmp/...)。

文件结构

/var/www/html/script.php
/var/www/html/tmp/01-55-34_561764e6cb06e/image1.jpg
/var/www/html/tmp/01-55-34_561764e6cb06e/image2.jpg
/var/www/html/tmp/01-55-34_561764e6cb06e/image3.jpg

我不想将我的脚本与图像放在同一个文件夹中!

找到

-j

--垃圾路径 只存储已保存文件的名称(垃圾路径),不要 存储目录名称。默认情况下,zip 将存储完整路径 (相对于当前目录)。