I've been using a simple script to export data to a .txt or .csv file. But I have noticed that my exports don't match others. And a friend has asked me to make adjustments to my code so he can upload what I export.

here is my simple export script:

<?
$contents = "Happy Buddy"."\t";
$contents .= "hold Circle"."\t";
$contents .= "Hollywood"."\t";
$contents .= "CA"."\t";
$contents .= "90210"."\n";


$contents .= "Tony Macker"."\t";
$contents .= "Crow Ave"."\t";
$contents .= "Hollywood"."\t";
$contents .= "CA"."\t";
$contents .= "90210"."\n";



header("Content-type: application/x-msdownload");
    header("Content-Disposition: attachment; filename=something.csv");
    header("Pragma: no-cache");
    header("Expires: 0");


print "$contents"; 
?>

This will export just fine, but it doesn't seem to add "commas" to the Comma Separated Value file! I must be missing something.

here is what gets exported:

Happy Buddy     hold Circle        Hollywood       CA      90210
Tony Macker     Crow Ave        Hollywood       CA      90210

This is what I'm trying to accomplish:

"Happy Buddy","hold Circle","Hollywood","CA","90210"
"Tony Macker","Crow Ave","Hollywood","CA","90210"

Any ideas of what I'm doing that is not quite exporting to the expected way?
From what my buddy tells me, there is a weird BOX-like character that shows up. I think it's carriage return, but it's a guess.

thanks to anyone with a better understanding on this!

    A CSV can be delimited by almost any character. This one happens to be by tabs.

    To get what you'd want, you'd have to escape the double-quotes (or encase the strings in single-quotes). Then, instead of "\t" at the end, use ",".

      If the file is opened in Windows notepad or similar ancient, DOS-based editor, it will expect "\r\n" as the line break. But "\n" should generally be more portable, and any reasonably modern version of MS Excel or editor like Wordpad should have no problem with it.

      As to writing the CSV, I would recommend the [man]fputcsv/man function, as it will handle the quoting/escaping issues when a field contains commas and/or double-quotes. (Note that fputcsv() requires PHP 5.1.0 or later.)

      header("Content-type: application/x-msdownload");
      header("Content-Disposition: attachment; filename=something.csv");
      header("Pragma: no-cache");
      header("Expires: 0");
      $fh = fopen('php://phpoutput', 'w'); // file handle will write to php output
      $contents = array(
         array(
            "Happy Buddy",
            "hold Circle",
            "Hollywood",
            "CA",
            "90210"
         ),
         array(
            "Tony Macker",
            "Crow Ave",
            "Hollywood",
            "CA",
            "90210"
         )
      );
      foreach($contents as $record)
      {
         fputcsv($fh, $contents);
      }
      fclose($fh);
      
        3 months later

        How in the world is that a tutorial? It's just giving you the code. A tutorial should really step you through it so that someone understands the code, not just knows that it works. Not to mention that it would just be easier to not write the file to disk first, then read it and send it to the browser. Just send the data to the browser. The Content-Type should more probably be "text/csv" instead of "application/csv".

        Just a couple things I saw that didn't have an explanation behind them as to why they were put in that "tutorial" the way they were.

          Agreed! I just had to chug through it and understand it for the use I needed. Tutorial, I think not but! But it does help a beginner coder that is only interested in copy/paste and not learning how it works. A tut would have been nice

            But it does help a beginner coder that is only interested in copy/paste and not learning how it works

            That's the attitude that is completely wrong if you're going to be coding.

            Give a man a fish, feed him for a day. Teach a man to fish, feed him for life.

            That's the mantra to go by. I really dislike articles/tutorials that just give you code. I also frown upon most posts that just give exorbitant amounts of code to someone rather than using a more socratic method to getting them to an answer. Now, some of my posts have been just code posts; however, I really kick myself now for doing that because neither they nor I learn from it.

            Either way, this is a fairly old posting, and I'm sure the OP has had their question(s) answered.

              bpat1434;10881400 wrote:

              That's the attitude that is completely wrong if you're going to be coding.

              I meant it sarcastically and agree 100% with the quote. I always like the learning aspect of looking at code in tut's, it helps you to achieve so much more in the long run that can be used to develop web sites further in the future!

              And I agree it's old. Im new to the forum and haven;t quite gotten the grasp of timelines. I've been in forums that take a month to get answers to people... Ill take better care of paying attention!

                Write a Reply...