Initiate PHP File download?

KevinO

[H]ard|Gawd
Joined
Aug 6, 2004
Messages
1,433
I am using the code below to try to initiate a download of an sql script created from the mysqldump command. I am using Apache as my webserver. Basically all it does is display the contents of the sql file in the browser (Both IE and Firefox) instead of displaying the Save / Open / Cancel dialog.

I am fairly new to this part of PHP and have tried manipulating the code via examples I found on the web with no success. Can anyone point me in the right direction?

I have tried changing the Content-Type: to different formats (application/x-download, etc) and adding set_time_limit(0); right before @readfile per some of the comments on @readfile on php.net.

Code:
# Start the download process
$len = filesize($backupFile);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public"); 
header("Content-Description: File Transfer");
header("Content-Type: application/text");
header("Content-Disposition: attachment; filename=$filename;");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$len);
@readfile($backupFile);
			
# Delete the temporary backup file from server
unlink($backupFile);
 
So replace @readfile with passthru? I tried some of the code examples on the php.net site for passthru (replaced my headers with theirs) and even put the exit(); after the passthru command and the page just sits there and loads with nothing showing up. The .sql file is only 3k in size.

Looks like I am on the right track. Thanks I will continue to look on passthru examples.
 
Back
Top