DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Matthew is a DZone employee and has posted 155 posts at DZone. You can read more from them at their website. View Full User Profile

Send A File Using Drupal And PHP

01.25.2009
| 4262 views |
  • submit to reddit
        // description of your code here

// load up the file and ship it off to the user
			$data = fopen($filepath,'rb');
			@ini_set('zlib.output_compression', 'Off'); 
			$http_headers = array (
			//      'Pragma: cache',
			//	'Expires: 0', - last one we tried
			// 'Pragma: public', - last one we tried
			//	'Cache-Control: no-cache, must-revalidate',
			//	'Cache-Control: private',
				'Pragma: no-cache',
				'Expires: Mon, 26 Jul 1997 05:00:00 GMT', // Need way in the past
				'Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT', 
				'Cache-control: must-revalidate, no-store, no-cache, post-check=0, pre-check=0',
				'Content-Transfer-Encoding: binary',
				'Content-Length:' . filesize($filepath),
				//'Content-Disposition: inline; filename="'.basename($filepath).'"',	
				'Content-Disposition: attachment; filename="'.basename($filepath).'"',	
				'Content-Type: application/octet-stream',
				'Content-Type: application/octetstream',
				'Content-Type: application/force-download',
				'Content-Type: application/download'
			);
			foreach ($http_headers as $header) {
				$header = preg_replace('/\r?\n(?!\t| )/', '', $header);
				drupal_set_header($header);
			}
	    
			while (!feof($data)) {
				print fread($data, 8096);
			}
			fclose($data);
			exit();