Link to home
Create AccountLog in
Avatar of Russ Cummings
Russ CummingsFlag for United States of America

asked on

How do I create a CSV file on a linux server that can be opened in Windows or Mac Excel?

I'm using cPanel / phpMyAdmin on a linux server for my website database.

I'm allowing select users with authorization to download canned tables to a .CSV.

All is working great for users that have a Windows OS, but for Mac users when they open the CSV file they tell me its blank. When I open the exact CSV file, it's fine.

Here is the code I use to output the data to a CSV file that gets sent to the user:

// output headers so that the file is downloaded rather than displayed
header("Content-Type: text/csv; charset=utf-8");
header("Content-Disposition: attachment; filename=results.csv");

// create a file pointer connected to the output stream
$output = fopen("php://output", "w");

// output the column headings
fputcsv($output, array("Record ID", "Date", "First", "Last", "Add 1", "Add 2", "City", "State", "Zip", "Phone", "Email", "Company", "Group", "Shirt", "Cemetery", "Section", "Spots", "Goldstar", "Placement", "Pickup", "P Section", "Leadership", "Email List", "Inactive", "Submitted"));
// fetch the data
//  if ($_SESSION['cemeteryname']<='') {
  $rows = mysql_query($_SESSION['resultvol']);

// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);

Is the problem with my code, or something with how the user is importing the file on the Mac?

Avatar of byundt
Flag of United States of America image

Presumably, people are using Mac Excel to open the .csv file. Because Windows users can open the file and see its data, the problem must be something associated with the Mac.

1. Are they double-clicking the file to make it open? If so, does the problem still exist if they use the File...Open ribbon item in Mac Excel?
2. Is the file stored in a protected folder? Can the users open other Excel files stored in that same folder? Try .xlsx, .xlsm, .xlsb, .csv and .txt file formats with this test.
3. Are users trying to run a macro to open the file? If so, the file must comply with Apple's sandboxing requirements. See Microsoft Excel MVP Ron de Bruin's article[/list]

Are you sure it's a plain text csv file?  Did you use line feed & carriage return, or did you only use line feed characters?

Do wc -l output.csv

How many lines are reported?
Avatar of Russ Cummings


I use the same general settings for Windows & Mac, nothing special - that I'm aware of.
The Mac user just opens the CSV file as he does other files he's importing data from.
If I import the same CSV file into Windows Excel, then save it as a Workbook the Mac user can open it, no problem.
do i do that within the php code?
will it display?
is it the last line of php code at the end - in my code?
Avatar of arnold
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account