Save table as csv in PHP

Why does this not prompt to download a file instead of just echoing to screen?
I have tried various header()
I tired print $csvoutput as well.

$csv_output = '"column 1","column2"';
$csv_output .= "\015\012";
$result = mysql_query("select * from results");

  while($row = mysql_fetch_array($result)) {
    $csv_output .= '"'.$row[firstname].'","'.$row[email].'"';
    $csv_output .= "\015\012";
  }

  //You cannot have the breaks in the same feed as the content.
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=yourfilename.csv");
header("Pragma: no-cache");
header("Expires: 0");
 echo $csv_output;
 // exit;
LVL 1
livegirlloveAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael701Commented:
try adding the length line of code, here's a snippet from code I've used.

     default:
          //Handle All Other Files
          $ContentType = 'application/octet-stream';
}
$strFileSize = filesize($strFilePath);

header("Content-Type: $ContentType);
header("Content-Length: $strFileSize");
header("Content-Disposition:attachment; filename=$strFileName");

echo $fileContents;
0
livegirlloveAuthor Commented:
No Difference.  Just prints to screen.  IE7 and Firefox2.

$strFileSize = filesize($csv_output);
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=yourfilename.csv");
header("Pragma: no-cache");
header("Expires: 0");
header("Content-Length: $strFileSize");
 print $csv_output;
  exit;

0
Michael701Commented:
use the string length since you have it in a local variable, sorry, i was reading data from a file.

$strFileSize = strlen($csv_output);
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

richdiesalProfessorCommented:
I encountered problems getting IE to recognize a download while Firefox was parsing correctly.  These work for me to download Word DOCs, but should work for you too:

$strFileSize = filesize($csv_output);
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="yourfilename.csv"');
header('Pragma: private');
header('Expires: 0');
header('Cache-control: private, must-revalidate');
header('Content-Transfer-Encoding: binary');
print $csv_output;
exit;
0
livegirlloveAuthor Commented:
no change.
Is it possible that it being in a protected directory has something to do with it?  
0
livegirlloveAuthor Commented:
oh wait didnt see the last post.
0
livegirlloveAuthor Commented:
Well after the last post still no dialog or save.

$csv_output = '"column 1","column2"';
$csv_output .= "\015\012";
$result = mysql_query("select * from results");

  while($row = mysql_fetch_array($result)) {
    $csv_output .= '"'.$row[firstname].'","'.$row[email].'"';
    $csv_output .= "\015\012";
  }

  //You cannot have the breaks in the same feed as the content.
$strFileSize = strlen($csv_output);
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="yourfilename.csv"');
header('Pragma: private');
header('Expires: 0');
header('Cache-control: private, must-revalidate');
header('Content-Transfer-Encoding: binary');
 print $csv_output;
  exit;
0
richdiesalProfessorCommented:
Try changing your content-type line to:
header("Content-type: application/vnd.ms-excel");
0
richdiesalProfessorCommented:
AND if that doesn't work, try
header("Content-type: application/force-download");
0
Michael701Commented:
you missed the content length header

header("Content-Length: $strFileSize");
0
livegirlloveAuthor Commented:
crazy.  Same problem.

Im going to try from a different computer in a different network just in case my isa server is blocking something.

$csv_output = '"column 1","column2"';
$csv_output .= "\015\012";
$result = mysql_query("select * from results");

  while($row = mysql_fetch_array($result)) {
    $csv_output .= '"'.$row[firstname].'","'.$row[email].'"';
    $csv_output .= "\015\012";
  }

  //You cannot have the breaks in the same feed as the content.
$strFileSize = strlen($csv_output);
header("Content-type: Content-type: application/force-download");
header('Content-Disposition: attachment; filename="yourfilename.csv"');
header('Pragma: private');
header('Expires: 0');
header('Cache-control: private, must-revalidate');
header('Content-Transfer-Encoding: binary');
header("Content-Length: $strFileSize");
 echo $csv_output;
  exit;
0
Michael701Commented:
also, just because, try it without the extra quotes around the filename, and a filename without spaces.

header('Content-Disposition: attachment; filename=yourfilename.csv');
0
richdiesalProfessorCommented:
Your question got me concerned enough - I started up Apache in development mode on my local machine, pasted your code, created a new CSV using "$csv_output = "1,2,3\n4,5,6\n7,8,9";, and tested it in both IE and Firefox - both worked!

Perhaps the CSV output is corrupt in some way?  Or headers are being blocked?
0
livegirlloveAuthor Commented:
Below code returns 1,2,3 4,5,6 7,8,9

How would headers be blocked?  PHP is NOT in safe mode if that means anything.

$csv_output = "1,2,3\n4,5,6\n7,8,9";
$strFileSize = strlen($csv_output);
header("Content-type: Content-type: application/force-download");
header('Content-Disposition: attachment; filename=yourfilename.csv');
header('Pragma: private');
header('Expires: 0');
header('Cache-control: private, must-revalidate');
header('Content-Transfer-Encoding: binary');
header("Content-Length: $strFileSize");
 echo $csv_output;
  exit;
0
richdiesalProfessorCommented:
What web browser are you testing this in?
0
livegirlloveAuthor Commented:
IE7 and Firefox 2.0.0.3
0
richdiesalProfessorCommented:
Same as me... now I'm just royally confused!

Let's try something else.

At the top of the file, enter:
header("Location: http://www.google.com");

If Google doesn't appear when you test the file, that means headers aren't being sent.
0
Michael701Commented:
ok, i posted that exact code on my go daddy hosted site and it prompts 'save as' dialog.

http://www.redboxcode.com/test.php

you try it, if still not prompting then it must be something in your local browser.

if it prompts, then it must be something in the php info settings.

michael
0
livegirlloveAuthor Commented:
google does not appear.

Hmm so why arent headers being sent?
0
livegirlloveAuthor Commented:
and your page prompts the #$@^$% dialog.

So what needs to change in the ini so i can ssh in and change it.
0
richdiesalProfessorCommented:
Frankly, this is by no means a normal problem.  It sounds like PHP and your webserver aren't interfacing correctly.  From Googling around a bit, it seems like it can occur in Apache when PHP is configured as CGI instead of as a Module...  but that might or might not be relevant.  What webserver are you using?  Apache?
0
Michael701Commented:
here's the data from the config setion of phpinfo(), i don't see anything about headers in the other sections.

PHP Version 4.3.11

System       Linux linhost110.mesa1.secureserver.net 2.4.21-27.ELsmp #1 SMP Wed Dec 1 21:59:02 EST 2004 i686
Build Date       Jan 11 2007 15:02:51
Configure Command       './configure' '--with-cgi' '--enable-fastcgi' '--with-config-file-path=/web/conf' '--with-gd' '--with-xml' '--with-gettext' '--with-zlib-dir=/usr/src/zlib' '--disable-posix' '--with-jpeg-dir=../jpeg-6b' '--enable-gd-native-ttf' '--enable-ftp' '--with-freetype-dir=/usr' '--with-freetype' '--with-sybase=/usr/src/freetds' '--with-curl=/usr/bin/curl' '--with-dom' '--enable-calendar' '--enable-soap' '--enable-bcmath' '--with-zip' '--with-openssl' '--with-mcrypt' '--with-mysql=/usr/local/mysql-5.0'
Server API       CGI/FastCGI
Virtual Directory Support       disabled
Configuration File (php.ini) Path       /web/conf/php.ini
PHP API       20020918
PHP Extension       20020429
Zend Extension       20021010
Debug Build       no
Thread Safety       disabled
Registered PHP Streams       php, http, ftp, https, ftps, compress.zlib
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
livegirlloveAuthor Commented:
Apache/2.0.52 (CentOS)
PHP 4.3.9-3.22.3
plesk 8.1
0
livegirlloveAuthor Commented:
'./configure' '--build=i686-redhat-linux-gnu' '--host=i686-redhat-linux-gnu' '--target=i386-redhat-linux-gnu' '--program-prefix=' '--prefix=/usr' '--exec-prefix=/usr' '--bindir=/usr/bin' '--sbindir=/usr/sbin' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib' '--libexecdir=/usr/libexec' '--localstatedir=/var' '--sharedstatedir=/usr/com' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--cache-file=../config.cache' '--with-config-file-path=/etc' '--with-config-file-scan-dir=/etc/php.d' '--enable-force-cgi-redirect' '--disable-debug' '--enable-pic' '--disable-rpath' '--enable-inline-optimization' '--with-bz2' '--with-db4=/usr' '--with-curl' '--with-exec-dir=/usr/bin' '--with-freetype-dir=/usr' '--with-png-dir=/usr' '--with-gd=shared' '--enable-gd-native-ttf' '--without-gdbm' '--with-gettext' '--with-ncurses=shared' '--with-gmp' '--with-iconv' '--with-jpeg-dir=/usr' '--with-openssl' '--with-png' '--with-pspell' '--with-xml' '--with-expat-dir=/usr' '--with-dom=shared,/usr' '--with-dom-xslt=/usr' '--with-dom-exslt=/usr' '--with-xmlrpc=shared' '--with-pcre-regex=/usr' '--with-zlib' '--with-layout=GNU' '--enable-bcmath' '--enable-exif' '--enable-ftp' '--enable-magic-quotes' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-track-vars' '--enable-trans-sid' '--enable-yp' '--enable-wddx' '--with-pear=/usr/share/pear' '--with-imap=shared' '--with-imap-ssl' '--with-kerberos' '--with-ldap=shared' '--with-mysql=shared,/usr' '--with-pgsql=shared' '--with-snmp=shared,/usr' '--with-snmp=shared' '--enable-ucd-snmp-hack' '--with-unixODBC=shared,/usr' '--enable-memory-limit' '--enable-shmop' '--enable-calendar' '--enable-dbx' '--enable-dio' '--enable-mbstring=shared' '--enable-mbstr-enc-trans' '--enable-mbregex' '--with-mime-magic=/usr/share/file/magic.mime' '--with-apxs2=/usr/sbin/apxs'
0
richdiesalProfessorCommented:
You're absolutely sure that no output is being sent to the page before the headers are?

And have you checked your Apache logs to make sure that it's not intercepting a malformed header somewhere?
0
livegirlloveAuthor Commented:
no and no

ill check that.

0
richdiesalProfessorCommented:
Headers cannot be sent after ANY output has been sent to the page itself.  Even if you echo ' '; before sending headers, those headers won't be sent.

As for the Apache logs, that should tell you if PHP is sending a header statement, Apache is receiving it, discarding it, and displaying the remainder of page how it wants.
0
Michael701Commented:
in your phpinfo what do you have for

display_errors      On

it's possible errors are not being displayed and you might have output before the headers

0
livegirlloveAuthor Commented:
PHP Warning: Cannot modify header information - headers already sent by (output started at /var/www/vhosts/soloenterprises.org/httpdocs/sampleagency/getcsv_clients.php:2) in /var/www/vhosts/soloenterprises.org/httpdocs/sampleagency/getcsv_clients.php on line 22

lots of them.  LOL

so lets start from scratch and try
0
livegirlloveAuthor Commented:
<?
header("Location: http://www.google.com");
?>

works!!

so...

It must be something in my DB.php file.
0
richdiesalProfessorCommented:
That should fix your problem then.  Headers MUST be sent before anything else.  It might be easier to link to an external download.php page with nothing but the CSV download.

And PSHEW!  That was close.  I was about to have you bust out a Telnet client.
0
livegirlloveAuthor Commented:
i tinkering now and will update.  im on a roll now lol.  tx
0
livegirlloveAuthor Commented:
works!!

Thanks for the help!!
0
richdiesalProfessorCommented:
Glad to help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.