[SOLVED]File download doesn't work in Google Chrome

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

[SOLVED]File download doesn't work in Google Chrome

Post by barb woolums »

I am using the following code to download a file from a my server to my pc. It works on other browsers but no save dialog comes up on google chrome. Just wondering if anyone else has experienced this and has found a solution.

Code: Select all

                                                                 $fp = fopen("exported-recipes.csv", "a");
						  
				  fwrite( $fp, $csv_output);
				  fclose($fp);
				  
				  $size_file=filesize("exported-recipes.csv");
				  
				  // We'll be outputting a csv file
				  header('Content-type: application/UTF-8');
					  
				  // It will be called exported-recipes.csv
				  header('Content-Disposition: attachment; filename="exported-recipes.csv"');
				  
				  //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.csv');
Last edited by barb woolums on Sat Nov 20, 2010 11:56 am, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: File download doesn't work in Google Chrome

Post by pickle »

It may be your Content-Type header. I've never seen "application/UTF-8" used. Usually for CSVs I use "text/csv". Since you're forcing a download, the type doesn't really matter a whole lot - but Chrome might be choking on it.

It's also slightly possible that Chrome isn't asking you to download, but is downloading it automatically for you. I know I encounter that behaviour sometimes in Safari - and Chrome shares the same engine.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply