Search code examples
phpmysqlsqlinto-outfile

How to force the user to download (Save as dialog) the file which just generated?


I'm generating a file using the query

SELECT * INTO OUTFILE " . "'c:/myfile.csv'" . " FIELDS TERMINATED BY ','  LINES TERMINATED BY '\n' FROM sample

My file is now in C:/myfile.csv. I want the user to download the file. How can I achieve this?


Solution

  • You could :

    • Send an HTTP header, to indicate the kind of data you're sending, and that it should be downloaded to a file.
    • And, then, send the content of that file.


    In PHP, something like this should do the trick :

    header('Content-Disposition: attachment; filename="my-file.csv"');  // The name which will be suggested to the user
    readfile('c:/myfile.csv');  // outputs the content of the file