N
N
NO2014-09-01 15:31:43
PHP
NO, 2014-09-01 15:31:43

How to get a link to download the archive?

There is a PHP code that creates a ZIP archive from files, and when the page is loaded (with this code), a window appears for downloading this archive.

# create new zip opbject
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.', '');
$zip->open($tmp_file, ZipArchive::CREATE);
# loop through each file
foreach($newArray as $file) {
  # download file
  $download_file = file_get_contents($file);
  # add it to the zip
  $zip->addFromString(basename($file), $download_file);
}
# close zip
$zip->close();
# send the file to the browser as a download
header('Content-disposition: attachment; filename=download.zip');
header('Content-type: application/zip');
readfile($tmp_file);

But I need the page to show a link to download the archive...
// I guess I need to do some magic on "header()"

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Taratin, 2014-09-01
@Mihail9575

Write on the page

<a href="путь до вашего php" download="имя которое получит ваш архив при скачивании.zip">Скачать архив</a>

S
Sergey, 2014-09-01
Protko @Fesor

Yes, remove the content-type and Content-disposition headers, remove readfile and replace it with a simple template with a link to the file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question