Forum Moderators: coopster

Message Too Old, No Replies

White Space When Downloaded CSV or TXT Files

         

leeparts

10:20 am on Sep 26, 2007 (gmt 0)

10+ Year Member



Hello All,
We have various php files the create either a csv of txt file from our database. We recently moved the site to a virtual dedicated server and that took us from php4 to php5. Now when we created a csv or txt file, we get white space at the top of the file. We have played around with the code, moved the headers and searched online to find various header configurations. No matter what we do, we still have 4-5 blank lines at the beginning of the file. Here are the header we are currently using for the csv file:

header("Content-type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=usps_export.csv");

Any Suggestions?

Thank you in advance

jatar_k

12:37 pm on Sep 26, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I would start by looking at the data it is using to create the file

then I would isolate the loop that gets/creates the data for the file and look at the data it is outputting before it writes

I wouldn't look at the headers right away

leeparts

4:05 pm on Sep 26, 2007 (gmt 0)

10+ Year Member



Thank you for your response. What I do not understand is how I set the header for the file like so:

echo "Full Name,Address,City,State,Zip Code,Country,E Mail,Reference Number\n";

Than I run the sql to grab the data, but the file output is four blank lines the header and than the data. I would think if the loop was the issue, the header would be line 1 and the data would start on line 6.

Make sense?

Demaestro

4:15 pm on Sep 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To set the file header move this

echo "Full Name,Address,City,State,Zip Code,Country,E Mail,Reference Number\n";

Outside the SQL loop.... I am assuming that it is inside the loop and is causing the empty lines... If it isn't in the loop can post more of the code?

leeparts

4:19 pm on Sep 26, 2007 (gmt 0)

10+ Year Member



It is outside of the loop. Here is some more code. Thanks for the reply.

echo "Full Name,Address,City,State,Zip Code,Country,E Mail,Reference Number\n";
$sqla="SELECT * FROM orders where order_status!='COMPLETE' and shipping_type='Priority' or order_status!='COMPLETE' and shipping_type='Express'";
#echo "<br>SQLA: $sqla<br>";
$resulta = mysql_query($sqla) or die('Query failed: ' . mysql_error());
$numresa=mysql_num_rows($resulta);
while ($linea = mysql_fetch_array($resulta, MYSQL_ASSOC)) {
$orderid=$linea["orderid"];
$customerid=$linea["customerid"];
$shipping_type=$linea["shipping_type"];

$sqlc="SELECT * FROM customers WHERE customerid='$customerid'";
#echo "SQLC: $sqlc<br>";
$resultc = mysql_query($sqlc) or die('Query failed: ' . mysql_error());
$numresc=mysql_num_rows($resultc);
while ($linec = mysql_fetch_array($resultc, MYSQL_ASSOC)) {
$name=$linec["name"];
$email=$linec["email"];
$address=$linec["address"];
$city=$linec["city"];
$state=$linec["state"];
$zip=$linec["zip"];
$country=$linec["country"];
$nametoship=$linec["nametoship"];
$addresstoship=$linec["addresstoship"];
$citytoship=$linec["citytoship"];
$statetoship=$linec["statetoship"];
$ziptoship=$linec["ziptoship"];
$countrytoship=$linec["countrytoship"];

echo "$nametoship,$addresstoship,$citytoship,$statetoship,$ziptoship,$countrytoship,$email,$orderid\n";
}

}

joelgreen

10:39 am on Sep 27, 2007 (gmt 0)

10+ Year Member



Do you have some newlines before the <?php tag?

Example file:
-- php file start ---

<?php
echo "123";
?>
-- php file end ---

Would output everything before the <?php tag, i.e. 2 lines.

Files like this may also cause troubles:
-- php file start ---
<?php
some php code here, for example database processing
?>

<?php
some more code here, for example CSV output
?>
-- php file end ---

leeparts

11:28 am on Sep 27, 2007 (gmt 0)

10+ Year Member



I did not have anything before the <?php. If I put the header for the csv file above the <?php than my output is the line 1 the header, lines 2-5 blank and line 6 starts the output.

This code is above the first sql:

Full Name,Address,City,State,Zip Code,Country,E Mail,Reference Number
<?php
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=usps_export.csv");
require_once("functions/page_functions.php");

Demaestro

2:29 pm on Sep 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



lee

Can you try this SQL from command line or MyAdmin and see if you get any rows returned where all the values are null or empty strings. It could be that the DB has empty rows? It is something to check.

SELECT * FROM customers

SELECT * FROM orders where order_status!='COMPLETE' and shipping_type='Priority' or order_status!='COMPLETE' and shipping_type='Express'

leeparts

3:17 pm on Sep 27, 2007 (gmt 0)

10+ Year Member



I have considered that also. There are no blank results in the database. I have a few different php files similar to this one. Some create a csv file and some create a txt file. They all produce the same whitespace. When running a shared server with php 4, I did not have this issue, now that the site is on a virtual dedicated server with php 5 this is happening. I have also tried it on various pc's, IE and firefox and different operating systems.

Demaestro

3:29 pm on Sep 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your code seems sound... these are the worst ones to track down.

Is it possible that some of the text fields have some bad chars in them.. like tab chars or newline chars... a lot of the time if you are outputting the sql to a command line or using an admin tool it will parse those out and you won't see them until they mess up code.... could have happened while remapping things over to your new server.

Are you able to check for such a thing? I am just throwing out ideas here...

[edited by: Demaestro at 3:31 pm (utc) on Sep. 27, 2007]

leeparts

3:42 pm on Sep 27, 2007 (gmt 0)

10+ Year Member



I ran just the first sql in a separate php file and I get no blank fields. The results of the code are going to change everyday, as the results are just orders that have a status of new, pending, etc, basically orders we still need to ship. Again I do not think the results are the issue, as the problem happens on other files also.

Demaestro

5:28 pm on Sep 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I set up the exact same thing on my server and it outputs correctly... I am not knowing what to say.

leeparts

5:32 pm on Sep 27, 2007 (gmt 0)

10+ Year Member



Perhaps I should contact GoDaddy?

Demaestro

6:03 pm on Sep 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Seems like a good idea.

I take it by the silence from anyone else that there are no more ideas about what this could be. That fact that it worked one place and not another concerns me.

leeparts

6:07 pm on Sep 27, 2007 (gmt 0)

10+ Year Member



Thank you to all that tried to help. I did try googling the issue prior to posting and found no results. If they reply with the answer I will post it.

rj2001

7:37 pm on Mar 14, 2008 (gmt 0)

10+ Year Member



Hopefully this fixes your problem. I had the same problem with whitespace at the top of all files including .txt. Make sure all your include files and the file that parses the download do not have spaces or new lines before or after <?php or ?>. I had 2 new lines after the ?> in a file that I used to call all my db queries, this was included in my download file script. The whitespace from the db file was the space that kept showing up in my downloaded files.

Also make sure that <?php or javascript stuff is at the very top of the page with no white spaces before them.

Hope that helps