Page 1 of 1

Content-length header ignored

Posted: Mon Feb 14, 2011 12:37 pm
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');

Re: Content-length header ignored

Posted: Mon Feb 14, 2011 12:54 pm
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.

Re: Content-length header ignored

Posted: Tue Feb 15, 2011 12:35 am
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?

Re: Content-length header ignored

Posted: Tue Feb 15, 2011 2:39 am
by barb woolums
Thanks mordred, that worked.

Re: Content-length header ignored

Posted: Tue Feb 15, 2011 2:43 am
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?

Re: Content-length header ignored

Posted: Tue Feb 15, 2011 2:51 am
by barb woolums
It's OK, I did a search and found out how to do it. Thanks a lot to both of you.