0

I have array and using that it makes a CSV but it is not saving it locally.

What should I change here?

Here is an array with code. Which is not working to create CSV on local system

$example_data = array(
  array('ID' => 1,'booktitle' => 'Quarter Share', 'author' => 'Nathan Lowell',
        'isbn' => '978-0982514542'),
  array('ID' => 2, 'booktitle' => '7th Son: Descent','author' => 'J. C. Hutchins',
        'isbn' => '0312384378'),
  array('ID' => 3, 'booktitle' => 'Shadowmagic', 'author' => 'John Lenahan',
        'isbn' => '978-1905548927'),
  array('ID' => 4, 'booktitle' => 'The Crown Conspiracy', 'author' => 'Michael J. Sullivan',
        'isbn' => '978-0979621130'),
  array('ID' => 5, 'booktitle' => 'Max Quick: The Pocket and the Pendant', 'author' => 'Mark Jeffrey',
        'isbn' => '978-0061988929'),
  array('ID' => 6, 'booktitle' => 'Jack Wakes Up: A Novel', 'author' => 'Seth Harwood',
        'isbn' => '978-0307454355')
);

function outputCsv($fileName, $assocDataArray)
{
    if(isset($assocDataArray['0'])){
        $fp = fopen('file.csv', 'w');
        fputcsv($fp, array_keys($assocDataArray['0']));
        foreach($assocDataArray AS $values){
            fputcsv($fp, $values);
            $data[]=$values;
        }

        $csv_handler = fopen ('file.csv','w');
        fwrite ($csv_handler,$data);
        fclose ($csv_handler);

        echo 'Data saved to csvfile.csv';

    }
}
Anthon
  • 69,918
  • 32
  • 186
  • 246
Rakhi
  • 929
  • 15
  • 41
  • What do you mean it is not saving locally? Do you mean locally to your machine or locally to your server? PHP runs server side, so file.csv should exist on your server in the same directory as the .php file containing the above code. – developer__c Jun 09 '15 at 09:56
  • local on my pc on locahost – Rakhi Jun 09 '15 at 11:04
  • PHP runs server side, but you could create a HTML link to the CSV file and then download it? http://stackoverflow.com/questions/2793751/how-can-i-create-download-link-in-html – developer__c Jun 09 '15 at 12:04

2 Answers2

0

I have changed just a bit of your function and is working for me :

function outputCsv($fileName, $assocDataArray)
{
    if(isset($assocDataArray['0'])){
        $fp = fopen($fileName, 'w');
        fputcsv($fp, array_keys($assocDataArray['0']));
        foreach($assocDataArray AS $values){
            fputcsv($fp, $values);
            $data[]=$values;
        }
/*
        $csv_handler = fopen ('file.csv','w');
        fwrite ($csv_handler,$data);
        fclose ($csv_handler);
*/
        fclose($fp); 
        echo 'Data saved to csvfile.csv';

    }
}

outputCsv('test.csv', $example_data);

You were trying to write array type data via fwrite() function and it expects string only. That is why it was generating error.

joy d
  • 408
  • 2
  • 13
0

the fwrite() function doesn't write array directly into a file first you have to do something like this (i edited your function)

function outputCsv($fileName, $assocDataArray)
{
    if(isset($assocDataArray['0'])){
        $data = array();
        $fp = fopen('file.csv', 'w');
        fputcsv($fp, array_keys($assocDataArray['0']));
        foreach($assocDataArray AS $values){
            fputcsv($fp, $values);
            $data[]=$values;
        }

        $csv_handler = fopen ('file.csv','w');
        fwrite ($csv_handler, print_r($data, true));
        fclose ($csv_handler);

        echo 'Data saved to csvfile.csv';

    }
}
Mohamed Belal
  • 610
  • 7
  • 11