1

I've learned how to create CSV files from MySQL data from another StackOverflow question. My problem is, for some reason when I call this code, it tries to save a file called index.php (which is the current page). Inside the index.php file my data from the table is there, separated by commas. I'm guessing I have a small typo somewhere, but after playing with the code I cannot find it. Thanks to anyone who can help.

$result=mysql_query("SELECT * from tbl_email");
if(mysql_num_rows($result)) {
  header ("Content-type: application/csv Content-Disposition:\"inline; filename=messages.csv\"");
  echo "REF #,Company,Name,Email,Message,Date\n";
  while($row = mysql_fetch_row($result)) {
    $companyname = mysql_query("SELECT company FROM tbl_users WHERE user_id ='$row[1]'");
    $datname = mysql_fetch_array($companyname);
    echo"$row[7],$datname[company],$row[2],$row[4],$row[5],$row[6]\n";
  }
  die();
}
4

2 に答える 2

3

1行に複数のヘッダーを提供する1つの呼び出しではなく、複数の呼び出しが必要ですheader()。CSVに最も適切なmimeタイプはですtext/csv

header("Content-type: text/csv");
header("Content-Disposition: inline; filename=messages.csv");

そして、より一般的にはContent-Disposition: attachment、ダウンロードを強制するために使用します。

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=messages.csv");
于 2012-07-01T03:29:54.030 に答える
0

It should be:

header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="messages.csv"');

Notice that the value for filename is not encased correctly with double quotes. Try to use single quotes in php, this will save you alot of trouble. ;)

Have a look at http://www.techcoil.com/blog/php-codes-to-tell-browsers-to-open-the-download-dialog-box-for-users-to-download-a-file/ to learn more about telling browser to download your file.

于 2012-07-01T03:40:44.760 に答える