Content-length header ignored

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Content-length header ignored

Post by barb woolums »

I am using the following code to create and download a file. It used to work, but now the 377kb file end up as an 800kb file whe downloaded, with the page html included at the end. Any ideas as to why this wouln't work all of a suden?

Code: Select all

$fp = fopen("exported-recipes.mmf", "a");
						  
				  fwrite( $fp, $mmf_output);
				  fclose($fp);
				  
				  $size_file=filesize("exported-recipes.mmf");
				  
				   header('Content-type: text/mmf');
					  
				  // It will be called exported-recipes.mmf
				  header('Content-Disposition: attachment; filename="exported-recipes.mmf"');
				  
				  //only output the contents of the file
				  header("Content-Description: Download PHP");
				  header("Content-Length: $size_file");
					  
				  // The CSV source is in exported-recipes.csv
				  readfile('exported-recipes.mmf');
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Content-length header ignored

Post by Mordred »

barb woolums wrote:Any ideas as to why this wouln't work all of a suden?
Basically, because that's what you told it to do ;)
An exit(); after the readfile() will stop the execution at that point and will not append the extra data to the file.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Content-length header ignored

Post by requinix »

Why are you using the exported-recipes.mmf file? You have the text right there in $mmf_output - just use that.

Plus, using a file creates concurrency problems. What if two people try to access that page at the same time?
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: Content-length header ignored

Post by barb woolums »

Thanks mordred, that worked.
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: Content-length header ignored

Post by barb woolums »

Hi tasairis,

I thought I had to use a file, so thanks for putting me straight. How would I do this though?
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: Content-length header ignored

Post by barb woolums »

It's OK, I did a search and found out how to do it. Thanks a lot to both of you.
Post Reply