Jump to content

New lines in CSV - very odd


Recommended Posts

Hi Guys


I'm getting something really strange.


Basically I have two files, one contains a link (to the other) and the other contains php that generates a simple csv.


Link php

echo "download your file <a href='http://localhost/creatingcsv/download.php'>HERE</a>";


CSV php


		//sample array
		$test = array(

			array('name', 'address'),
			array('bilbo', 'bagins')

		//Generate headers for csv
		$implosion = implode(',',$test[0]);

		//Add new line
		$implosion .= "\n";

		//Generate csv row of data
		$implosion.= implode(',', $test[1]);

		//call function to output data

		function download($implosion){

		header("Content-type: text/csv");
		header("Content-Disposition: attachment; filename=file.csv");
		header("Pragma: no-cache");
		header("Expires: 0");

		echo $implosion;




Nothing radical there.


But in the csv php file if the <?php tag isn't on line one then the csv generates with new lines above the header data - in the actual csv file that is output.


So if I go into csv php file and hit return five times, above the <?php tag, I end up with five empty rows before the data in the csv.


Can anyone tell me why this is happening?



Link to comment
Share on other sites

Because with output buffering, PHP basically "holds on to" any output until the end of script execution (or until the output buffering is otherwise ended).


It is common practice to omit the closing PHP tag (?>) to prevent any accidental white-space. The closing tag is not required.

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.

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.