本文介绍了Zip 中没有文件,但解压缩 zip 时,里面有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处寻找问题,但没有找到类似的问题.

I searched for the problem everywhere but I didn't find similar problem.

我使用以下代码创建了一个 zip 文件.zip文件的容量似乎是正常的,因为它有文件.当我双击它时,我可以看到我的 zip 中的文件.但是当我解压 zip 文件时,我看到这些文件已添加到两个不同的目录中,我的文件也是如此.

I create a zip file with the following code. The capacity of the zip file seems to be normal as it has file. I can see the files in my zip when I double click it. But when I extract the zip file, I see that the files have been added in two different directories and my files too.

让我在下面更详细地解释.

Let me explain in more detail below.

$files = array(
          'bauhaus93regular-1519481745.382.ttf' => '../../../files/task_files/bauhaus93regular-1519481745.382.ttf',
          'dsc_0299-1518894123.175.jpg' => '../../../files/task_files/dsc_0299-1518894123.175.jpg',
          'sertifika-1518371071.2923.pdf' => '../../../files/task_files/sertifika-1518371071.2923.pdf'
     );

$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $key=>$file) {
  $zip->addFile($file, $key);
}
$zip->close();

header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($zipname));
ob_clean();
readfile($zipname);
ob_flush();

zip 文件状态图像

有了这个,我可以将文件下载为 file.zip.如果我点击两次,我的文件就在里面.但它的大小是 14,9 MB.当我解压 zip 时,我可以看到我所有的文件,但是这些文件在两个不同的目录中并且与它们在一起;

With this I can download the file as file.zip. If I click twice, my files in it. But it's size is 14,9 MB. When I extract the zip, I can see all my files, but the files are in two different directories and with them;

files/task_files/(my_files)
xampp/htdocs/contract/files/task_files/(my_files)
(my_files)

提取的 zip 文件图像

第一个目录是我的本地主机地址,第二个是我的硬盘驱动器和我的文件上的物理目录.

First directory is my localhost adress and second one is phisical directory on my harddrive and my files again.

如果你能帮到我,我很高兴.提前致谢.

I am very glad if you help. Thanks in advance.

请注意;PHP 版本是 5.7 因需要.

Note that; PHP version is 5.7 due to necessity.

推荐答案

尝试传递可选的 本地文件名

$files = array(
              'bauhaus93regular-1519481745.382.ttf' => '../../../files/task_files/bauhaus93regular-1519481745.382.ttf',
              'sertifika-1518371071.2923.pdf' => '../../../files/task_files/dsc_0299-1518894123.175.jpg',
              'sertifika-1518371071.2923.pdf' => '../../../files/task_files/sertifika-1518371071.2923.pdf'
         );

$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE | ZipArchive::OVERWRITE);
foreach ($files as $key=>$file) {
  $zip->addFile($file, $key);
}
$zip->close();

这篇关于Zip 中没有文件,但解压缩 zip 时,里面有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 08:48