Home > Backend Development > PHP Tutorial > How to Download a CSV file from a PHP Array?

How to Download a CSV file from a PHP Array?

Linda Hamilton
Release: 2024-11-08 16:57:02
Original
436 people have browsed it

How to Download a CSV file from a PHP Array?

How to Create and Download a CSV File from a PHP Script

Creating and downloading a CSV file from a PHP array is a useful technique in website development. Here's a detailed guide for novice programmers:

Creating the CSV File

  1. Loop Through the Array: Iterate over the elements of your PHP array.
  2. Create CSV Lines: For each array element, use the fputcsv() function to generate a comma-separated value (CSV) line. This function takes the array element and a delimiter (usually a comma) as parameters.

Example:

$array = [
    ['fs_id' => '4c524d8abfc6ef3b201f489c', 'name' => 'restaurant', ...],
    // More array elements...
];

$delimiter = ',';

$csv = fopen('tmp.csv', 'w');

foreach ($array as $line) {
    fputcsv($csv, $line, $delimiter);
}
Copy after login

Downloading the CSV File

  1. Set HTTP Headers: To make the browser offer a "Save As" dialog, set HTTP headers that specify the file name and its type as CSV.
header('Content-Disposition: attachment; filename="filename.csv"');
header('Content-Type: text/csv');
Copy after login
  1. Send the CSV to the Browser: Use the fpassthru() function to send the generated CSV lines to the browser.
fseek($csv, 0); // Reset the file pointer to the start
fpassthru($csv);
Copy after login

Putting It All Together

The following function combines both steps and allows you to download a CSV file from an array:

function array_to_csv_download($array, $filename = 'export.csv', $delimiter = ',') {
    // Set HTTP headers
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    header('Content-Type: text/csv');

    // Create a file pointer
    $csv = fopen('php://memory', 'w');

    // Loop through the array and create CSV lines
    foreach ($array as $line) {
        fputcsv($csv, $line, $delimiter);
    }

    // Send the generated CSV to the browser
    fpassthru($csv);
}
Copy after login

Usage:

$array = [
    ['fs_id' => '4c524d8abfc6ef3b201f489c', 'name' => 'restaurant', ...],
    // More array elements...
];

array_to_csv_download($array, 'restaurants.csv');

// The CSV file will be downloaded to the user's computer.
Copy after login

Additional Note:

As an alternative to using php://memory, you can also use php://output for the file descriptor, which may be more efficient for large datasets.

This method provides a straightforward way to create and download CSV files from PHP arrays, making it a valuable tool for website developers.

The above is the detailed content of How to Download a CSV file from a PHP Array?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template