When a PHP script shall return a (downloadable) file instead of HTML, the correct HTTP headers have to be sent:
The preceding code reads in a ZIP file and sends it to the client; Figure 6.6 shows its result in the browser. Sending a File with HTTP (httpfile.php)<?php $filename = 'httpfile.zip'; $mimetype = 'application/zip'; $data = file_get_contents($filename); $size = strlen($data); header("Content-Disposition: attachment; filename = $filename"); header("Content-Length: $size"); header("Content-Type: $mimetype"); echo $data; ?> Figure 6.6. The browser wants to save the file.
|