-1

I am developing a script using PHP to save a file to desktop as .csv file.

I found this code sample from PHP Manual.

<?php
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>  

I created file.csv on my desktop. My issue is I cannot find the correct path to open the file on the desktop and, write and save. ($fp = fopen(‘file.csv’, ‘w’))

Should I have to show the path to server where I stored my web site and from there to to desk top's file.csv?

Can somebody guide me?

Thank you very much.

user1427195
  • 43
  • 4
  • 10

4 Answers4

4
<?php
$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);
header('Content-type: text/csv');
header("Content-Disposition: attachment;filename=file.csv");

// stream
$f  =   fopen('php://output', 'a');
foreach ($list as $fields) {
    fputcsv($f, $fields);
}
Amado Martinez
  • 429
  • 2
  • 8
  • Dear Amado Martinez, Thanks for the great suggestion. It is great but, the code save all the PHP and HTML codes I wrote on the page. Is there a way to write down only aaa,bbb,ccc,dddd 123,456,789 "aaa","bbb" Thank you very much – user1427195 Sep 08 '12 at 19:37
  • Thank you very much. With help of yours and Alain T's help I could solve the issue. Thanks again – user1427195 Sep 09 '12 at 00:25
1

If you speak about the client's desktop, you can't using this way.

Anyway, why not giving your client a way to download your file?

Update: made a small improvment from Amado Martinez's answer

<?php
    $list = array (
        array('aaa', 'bbb', 'ccc', 'dddd'),
        array('123', '456', '789'),
        array('"aaa"', '"bbb"')
    );

    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header('Content-type: text/csv');
    header("Content-Disposition: attachment;filename=file.csv");
    header("Content-Transfer-Encoding: binary");

    $fp = fopen('php://output', 'a');
    foreach ($list as $fields) {
        fputcsv($fp, $fields);
    }
    fclose($fp);

?>
Alain Tiemblo
  • 36,099
  • 17
  • 121
  • 153
  • instead of saving to file.csv, you could stream the csv. – Amado Martinez Sep 08 '12 at 18:53
  • Thank you very much for your suggestion. I think that should be the way. I will check your answer and let you know. – user1427195 Sep 08 '12 at 19:19
  • Dear Alain T. It works. But there is a issue there. After I saved the file.csv it has grab all the codes. (HTML codes I written on the template to create the page) Is there a way to get only aaa, bbb, ccc, dddd 123, 456,789 "aaa","bbb" Thank you very much – user1427195 Sep 08 '12 at 19:27
  • Yes, in your current php file, add a link: `download`, and in generate_csv.php, you put all your csv logic without anything else. – Alain Tiemblo Sep 08 '12 at 19:39
  • Dear Alain T, Thanks for the answer. I will try it and let you know. Thanks again – user1427195 Sep 08 '12 at 19:44
0

You cannot save to your desktop! Your browser runs in a sandbox, a sanitized environment that prevents access to the filesystem .

JvdBerg
  • 21,777
  • 8
  • 38
  • 55
0

You script runs on server. Your file is on desktop. And even you got local server (like xamp, uniformserver etc) these are (and should be) also separated from your whole host filesystem. So the answer is - you cannot reach that unless you run your php script from command line on your host.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141