1

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.

mikelovelyuk
  • 4,042
  • 9
  • 49
  • 95
  • show your command you run in your command line – Saty Apr 07 '15 at 10:42
  • 1
    You can't "download" anything using the command line in the browser sense - there is no HTTP client for that. You need to write the file to a folder or set destination when running from the command line. – Dan Smith Apr 07 '15 at 10:42
  • @Bulk - Yep. I was being a little optimistic and generally confused about the client/server side divide. – mikelovelyuk Apr 07 '15 at 11:01
  • 1
    Yeah I think you might be - the command line is for direct interaction with the server - it's not something you want clients involved with generally :) – Dan Smith Apr 07 '15 at 11:02

2 Answers2

1

If you display the content of the CSV on the index.php file, you can do this command:

php index.php 2014 > my_csv.csv

Akram Fares
  • 1,653
  • 2
  • 17
  • 32
1

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

$fp = fopen($$filepath, "w");
Sanjay Kumar N S
  • 4,653
  • 4
  • 23
  • 38
  • I had that initially.. And you are correct. But it would download to the server. Not the client. – mikelovelyuk Apr 07 '15 at 10:49
  • Yes file will be generated in the system whose terminal the script is running. What you meant by client machine in this case? – Sanjay Kumar N S Apr 07 '15 at 10:51
  • I mean, so the user can save it to their desktop for example. But judging by the responses I am getting on this question, I am starting to understand that it may not be possible. Which is fine – mikelovelyuk Apr 07 '15 at 10:58
  • Then the php script should generate the csv and return the filepath, so that from the client machine you can download from the server side using another php code. – Sanjay Kumar N S Apr 07 '15 at 11:01