ZipArchive PHP只添加整个路径中没有目录的文件

Igo*_* L. 7 php ziparchive

研究员:

讨厌问这个问题,我相信正确回答了两次:

  1. 这里

  2. 和这里

所以问题是:当解压缩下载的'file.zip'时,在解压缩后,它包含路径中的所有文件夹,即使很难,我在addFile函数中指定了本地文件.我希望zip文件不包含任何子文件夹,只包含文件.

$zipname = str_replace(' ', '_', $obj['Name']) . '.zip';

$zip = new ZipArchive();

$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
    // $file looks like this: webroot/uploadify/files/file.jpg
    $zip->addFile($file, pathinfo($file, PATHINFO_BASENAME));
}

$zip->close();

header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=" . $zipname);
header("Pragma: no-cache");
header("Expires: 0");
header('Content-Length: ' . filesize($zipname));

readfile($zipname);
Run Code Online (Sandbox Code Playgroud)

任何人都可以看到这个代码的问题是什么?

附加信息:

我,ofc,使用相对于webroot的路径,但解压后的文件夹中的文件夹层次结构:extract to file - >'my_file.zip'是C-> xampp-> htdocs-> my_cakephp_web_app-> app-> webroot->文件不是我想要实现的:)

PHP 5.4.4

Windows XP,XAMPP

编辑

尝试使用http://www.phpconcept.net/pclzip/库并遇到同样的问题.在我的家用电脑(win7)上,这段代码运行良好.

这提出了一个问题:我有什么设置我不应该知道吗?

Ada*_*son 2

您需要测试每个文件以查看它是否是一个文件(而不是目录、符号链接等):

foreach ( $files as $file )
{
    if(filetype($file) == 'file') {
        $zip->addFile( $file, pathinfo( $file, PATHINFO_BASENAME ) );
    }
}
Run Code Online (Sandbox Code Playgroud)

这是使用filetype,但您也可以使用is_file