0

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.

Pecado
  • 35
  • 7
  • "If I click twice there isn't any file in it." -- That's a very coarse observation. How about explaining it a bit? With a screencast if you must? Tried another ZIP tool yet? Zip testing? Actual header debugging (cmdline wget/curl)? Tried reinstalling Windows (have to assume from the vague description that you're a Windows user)? Or if this is about the PHP code: what does error_reporting say? How does it behave on the CLI? – mario Feb 25 '18 at 09:38
  • Thank you for your comment. But I have never been so badly interpreted. I'm sorry, my English is inadequate :) I put some image on my question. I tried error_reporting but zip download browser window closes so I can't see the error message. – Pecado Feb 25 '18 at 12:13
  • Try moving these files in same directory as that of php file and modify the `$files = array('file1.pdf','file2.tff');` – Vinay Feb 25 '18 at 12:38

1 Answers1

0

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();
Vinay
  • 7,442
  • 6
  • 25
  • 48
  • Thank you Viney. I edited my post and first problem is solved. I can see my files in a zip file normally. But when I extracted the zip file, two directory and my files shown. Files in each directory have been copied. Copied 3 times in total. I updated images. Do you think there is a cache problem? – Pecado Feb 25 '18 at 13:23
  • Yes it's sorta "caching" `ZipArchive::OVERWRITE` will remove the older versions of the zip file so php will always re-create without this flag php by default will try to use the older zip file if it already exists. – Vinay Feb 25 '18 at 15:19