简体   繁体   中英

save csv file on local machine using php

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';

    }
}

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.

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';

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM