ChrisMartino Posted June 7, 2011 Share Posted June 7, 2011 Hey there! Thanks for taking the time to read my thread. So I have an issue with downloading a local file to the clients computer. When I call the header functions to prompt the download for the user it downloads part of the pages content as well and this is not required. I simply want to download the file specified without any additional content. Now I'm aware that it is most likely doing this because I'm calling the download prompt headers from within the center of a HTML/PHP document but this is required and there is no possible way of changing the method of execution. Here is my function: static function download_file($file_path) { $file_name = explode("/", $file_path); $save_directory = str_replace("Frontend/index.php", "Temporary/", $_SERVER['SCRIPT_FILENAME']); if(!ftp_get(self::$FTP_Handle, $save_directory.end($file_name), $file_path, FTP_BINARY)) die("failed."); if(file_exists($save_directory.end($file_name))) { header("Content-type: application/force-download"); header('Content-Disposition: inline; filename="'. end($file_name) .'"'); header("Content-Transfer-Encoding: Binary"); header("Content-length: ".filesize($save_directory.end($file_name))); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . $save_directory.end($file_name) . '"'); readfile($save_directory.end($file_name)); } return 1; } Now the supplied function also downloads part of the page source as well as the destination files source when called from a page with prior HTML content. Is there any way to stop this happening without corrupting the file as it may be LINUX executables being downloaded at certain points. Thanks for your time, Christopher. Quote Link to comment https://forums.phpfreaks.com/topic/238606-php-file-download-wheaders/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 7, 2011 Share Posted June 7, 2011 A) You apparently have output_buffering turned on or you are specifically using ob_start() in your script, and B) You cannot output a download file (or binary image data) directly on web page. The only thing that you can output on the page request that causes the download are the headers followed by the file data being downloaded. Quote Link to comment https://forums.phpfreaks.com/topic/238606-php-file-download-wheaders/#findComment-1226198 Share on other sites More sharing options...
ChrisMartino Posted June 7, 2011 Author Share Posted June 7, 2011 A) You apparently have output_buffering turned on or you are specifically using ob_start() in your script, and B) You cannot output a download file (or binary image data) directly on web page. The only thing that you can output on the page request that causes the download are the headers followed by the file data being downloaded. Thanks! I updated the header format for the file type and prior to setting the headers called ob_end_clean and the file downloaded correctly without an issue! Thanks for your time Quote Link to comment https://forums.phpfreaks.com/topic/238606-php-file-download-wheaders/#findComment-1226203 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.