Jump to content

PHP File Download W/Headers.


ChrisMartino

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.