PHP: Why do I get blank lines at the top of my downloaded file?

Trying to send a file using a PHP script, the file gets blank lines ad

Page 1 of 1

3 Replies - 2485 Views - Last Post: 21 May 2009 - 09:09 AM Rate Topic: -----

#1 Tarliman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-March 09

PHP: Why do I get blank lines at the top of my downloaded file?

Post icon  Posted 21 May 2009 - 07:39 AM

I'm generating a text file in parallel with my print routine. The file generates just fine, creating a tab-delimited file that loads into Excel just like I want it to. The problem occurs when I try to download the file from a button.

A clip from the file generation routine:

	$excelout = fopen("genreport.xls", "w") or die ('Open file for Excel export failed. Notify development.');
	$excelheader = $tblheader . "\n";
	fwrite($excelout, $excelheader) or die ('Failed to begin writing Excel file. Notify development.');

		$outtext = "Peregrine Service Center search results" . "\n";
		fwrite($excelout, $outtext);
		$outtext = "Ticket#" . "\t" . "Opened By" . "\t" . "Opened Date" . "\t" . "Restored Date" . "\t" . "Brief Description" . "\n";
		fwrite($excelout, $outtext);
		$dispcount=0;
		while ($tktrow = mysql_fetch_array($tktresult, MYSQL_ASSOC)) {
			/* add tktrow to output array */
			$disparray[] = $tktrow;
			/* format output array and write it to the Excel file */
			$outtext = $disparray[$dispcount]['tktnum'] . "\t" . $disparray[$dispcount]['tktopenedby'] . "\t" . $disparray[$dispcount]['tktopentime'] . "\t" . $disparray[$dispcount]['tktrestoretime'] . "\t" . $disparray[$dispcount]['tktbriefdesc'] . "\n";
			fwrite($excelout, $outtext);
			$dispcount++;
		}
		$outtext="\n";
		fwrite($excelout, $outtext);

	fclose($excelout);




The button:

<form><input type=\"submit\" name=\"Submit\" value=\"Export Excel\"></form>


The code it calls:

	} elseif ($_REQUEST['Submit'] == "Export Excel") {
		excelDown();


function excelDown() {
	header("Content-type: application/msexcel");
	header("Content-Disposition: attachment; filename=genreport.xls");
	header("Content-Transfer-Encoding: binary");
	header("Cache-Control: private", false);
	header("Pragma: no-cache");
	header("Expires: 0");
	readfile("genreport.xls");



When I click the button, the script sends me the file, but adds nine blank lines to the top of the file. Why is this?

Is This A Good Question/Topic? 0
  • +

Replies To: PHP: Why do I get blank lines at the top of my downloaded file?

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3077
  • View blog
  • Posts: 10,790
  • Joined: 08-August 08

Re: PHP: Why do I get blank lines at the top of my downloaded file?

Posted 21 May 2009 - 08:19 AM

What is $tblheader?
Was This Post Helpful? 0
  • +
  • -

#3 Tarliman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-March 09

Re: PHP: Why do I get blank lines at the top of my downloaded file?

Posted 21 May 2009 - 08:41 AM

View PostCTphpnwb, on 21 May, 2009 - 07:19 AM, said:

What is $tblheader?

In the test instance, $tblheader = "Tickets for technician.name for 2008-11 Total Count for Trading and Non-Trading combined\n"

Bear in mind that the text file that is written does not have blank lines at the top. The blank lines are being inserted when I try to download the file via PHP script.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3077
  • View blog
  • Posts: 10,790
  • Joined: 08-August 08

Re: PHP: Why do I get blank lines at the top of my downloaded file?

Posted 21 May 2009 - 09:09 AM

Are you sure? It's the first thing (that we can see) that gets written to the file, so it would seem that there might be a couple of "\n" in it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1