php code to save files on user machine

August 11, 2008 · Filed Under PHP · Comment 

I was trying to save the '.xls', '.doc', '.wmv' on user machine, which works correctly on Firefox(developer friendly) but not on Microsoft's IE(Microsoft friendly);

after searching on the net, i found this code which works on both the browser.

here is the code:
-------------------------
<?php
// downloading a file
$filename = $_GET['path'];

// fix for IE catching or PHP bug issue
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// browser must download file from server instead of cache

// force download dialog
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");

// use the Content-Disposition header to supply a recommended filename and
// force the browser to display the save dialog.
header("Content-Disposition: attachment; filename=".basename($filename).";");

/*
The Content-transfer-encoding header should be binary, since the file will be read
directly from the disk and the raw bytes passed to the downloading computer.
The Content-length header is useful to set for downloads. The browser will be able to
show a progress meter as a file downloads. The content-lenght can be determines by
filesize function returns the size of a file.
*/
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));

@readfile($filename);
exit(
0);
?>

PHP Script to load a PDF

August 11, 2008 · Filed Under PHP · Comment 

To get PHP to load a PDF (for example) from file, use the following code.

<?php
$filename
= $_SERVER['DOCUMENT_ROOT'] . "/path/to/file/my_file.pdf";
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.basename($filename));
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($filename));
readfile($filename);
?>

Cannot modify header information – headers already sent

August 11, 2008 · Filed Under PHP · Comment 

Warning: Cannot modify header information – headers already sent by (output started at /home/domain/public_html/file.php:42) in home/domain/public_html/includes/main.php on line 302

Solution

The “headers already sent” error is usually caused by having white space before or after the opening and closing PHP tags (<?php . . . ?>).

Remove the additional spaces , this should solve the problem

Header already sent

August 11, 2008 · Filed Under PHP · Comment 

Note that if you don't want to go through the process of making sure that there is no output before you send a header, you can use

<?php
ob_start
();
?>

at the beginning of your page.
This starts the output buffer, which allows you to send headers whenever you feel like it. Make sure that you put it at the BEGINNING, after the first php tag.

It allows you to do something like

<?php
ob_start
();
echo
'Hello';
header("Status: 404 Not Found");
echo
'Bye';
?>