$filename = 'httpfile.zip'; header("Content-Disposition: attachment; filename = $filename");
When a PHP script shall return a (downloadable) file instead of HTML, the correct HTTP headers have to be sent:
Content-Dispositionfor the (proposed) name of the file
Content-Lengthfor the file size
Content-Typefor the MIME type of the file
The preceding code reads in a ZIP file and sends it to the client; figure shows its result in the browser.
Sending a File with HTTP
<?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; ?>
The browser wants to save the file.
What Does PEAR Offer?
The following PEAR packages offer functionality that can be used for working with files and streams:
Fileoffers some helper functions for file access, some of the deprecated to new functionality in more recent PHP releases
File_Findsearches a path for certain files or patterns
File_SearchReplacedoes a Search and Replace within files
Stream_Varallows you to save variables in streams, so you can access them like you would access files or streams