Search code examples
phpcommand-lineargvfputcsv

Download file as csv when running script from command line


When I run my php script from a browser, I get a csv file to automatically download to the client using $fp = fopen('php://output', "w");, fputcsv and the appropriate header tags. It works very nicely.

When I run my script from the command line using php index.php 2014 ("2014" being the argv I am passing in), then the contents of what would normally go in to the csv actually appears in the command line box.

How can I get my code to still download a csv file when using the command line?

If it matters I am running ubuntu using Vagrant and VirtualBox.


Solution

  • You can update the line as follows, where $$filepath is the file path name.

    $fp = fopen($$filepath, "w");