There is no file in Zip but when extract zip, there are files in it

有些话、适合烂在心里 提交于 2019-12-11 16:40:06

问题


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

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 status image

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)

extracted zip file image

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.

Note that; PHP version is 5.7 due to necessity.


回答1:


Try passing the optional local filename

$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();


来源:https://stackoverflow.com/questions/48971809/there-is-no-file-in-zip-but-when-extract-zip-there-are-files-in-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!