Memon Posted January 30, 2010 Share Posted January 30, 2010 Hello All I want to put a link (or button) which makes possible to download the CSV file to local PC . Converting in excel format will be more useful, but not necessary. Quote Link to comment https://forums.phpfreaks.com/topic/190324-downloading-csv-file-to-local-pc/ Share on other sites More sharing options...
trq Posted January 30, 2010 Share Posted January 30, 2010 See the Faq/Snippet repo board. There is a post in there called 'force download' with an example of such a script. Quote Link to comment https://forums.phpfreaks.com/topic/190324-downloading-csv-file-to-local-pc/#findComment-1004093 Share on other sites More sharing options...
Memon Posted January 31, 2010 Author Share Posted January 31, 2010 I tried with that code, but it didn't work. Then I searched other sites & made some changes (special line for IE etc), but it still does not work. It gives 6 warnings (for each header line), saying; Warning: Cannot modify header information - headers already sent by (output started at D:\My Documents\My Webs\test.php:6) in D:\My Documents\My Webs\test.php on line 18 Then it gives following warning; Warning: readfile(stock.csv) [function.readfile]: failed to open stream: No such file or directory in D:\My Documents\My Webs\test.php on line 24 Can anybody guide me, where I am mistaking. My script is as follows; <?php $fp = "test.csv"; echo "<P ALIGN='CENTER'><font face='Arial Black' size='5' color='#0000FF'>File Name : $fp</FONT></P>"; // make sure it is a file before doing anything! if(is_file($fp)) { // required for IE, otherwise Content-disposition is ignored if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header( "Content-Disposition: attachment; filename=".basename($fp)); header( "Content-Description: File Transfer"); readfile($file); echo "<P ALIGN='CENTER'><font face='Arial Black' size='5' color='#00FF00'>SUCCSESS!</FONT></P>"; exit(); } else { echo "<P ALIGN='CENTER'><font face='Arial Black' size='5' color='#FF0000'>ERROR! NO SUCH FILE.</FONT></P>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/190324-downloading-csv-file-to-local-pc/#findComment-1004476 Share on other sites More sharing options...
trq Posted January 31, 2010 Share Posted January 31, 2010 Why are you echoing html? Quote Link to comment https://forums.phpfreaks.com/topic/190324-downloading-csv-file-to-local-pc/#findComment-1004547 Share on other sites More sharing options...
Memon Posted January 31, 2010 Author Share Posted January 31, 2010 Why are you echoing html? For testing purpose. To see the result. Is that making problem? Quote Link to comment https://forums.phpfreaks.com/topic/190324-downloading-csv-file-to-local-pc/#findComment-1004588 Share on other sites More sharing options...
premiso Posted January 31, 2010 Share Posted January 31, 2010 Is that making problem? Yes, headers cannot be sent if output has already been sent to the browser, it would cause an error. To generally tests scripts like these I would write data to a "log" file instead, that way you can see the data in the log file and it won't break the script. Quote Link to comment https://forums.phpfreaks.com/topic/190324-downloading-csv-file-to-local-pc/#findComment-1004615 Share on other sites More sharing options...
Memon Posted January 31, 2010 Author Share Posted January 31, 2010 OK. I removed all "echo"s. My Script is now like this. <?php $fp = "test.csv"; if(is_file($fp)) { // required for IE, otherwise Content-disposition is ignored if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header( "Content-Disposition: attachment; filename=".basename($fp)); header( "Content-Description: File Transfer"); readfile($fp); exit(); } ?> But now it outputs the whole CSV file on screen, instead of downloading it. Still some mistake in my code. Quote Link to comment https://forums.phpfreaks.com/topic/190324-downloading-csv-file-to-local-pc/#findComment-1004766 Share on other sites More sharing options...
Memon Posted February 2, 2010 Author Share Posted February 2, 2010 Same code is now working fine. But rarely, it prints the whole text file on screen, instead of downloading. But running next time, again it works fine. Anyway, my problem is solved, as mostly it is working. Quote Link to comment https://forums.phpfreaks.com/topic/190324-downloading-csv-file-to-local-pc/#findComment-1005301 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.