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

How to Programmatically Download CSV Files from a PHP Array?

DDD
Release: 2024-11-10 13:33:02
Original
902 people have browsed it

How to Programmatically Download CSV Files from a PHP Array?

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

Overview

This article aims to provide a comprehensive solution for creating and downloading a CSV (Comma-Separated Values) file from a PHP array. As a novice programmer, you may need assistance implementing this functionality.

Generating CSV Lines

To generate CSV lines from your PHP array, you can utilize the built-in fputcsv() function, which converts arrays into formatted strings suitable for CSV:

$f = fopen("tmp.csv", "w");
foreach ($array as $line) {
    fputcsv($f, $line);
}
Copy after login

HTTP Headers for Download

To initiate a file download from the browser, sending appropriate HTTP headers is crucial. One such header is:

header('Content-Disposition: attachment; filename="filename.csv";');
Copy after login

This header informs the browser that the response contains a file attachment with the specified filename.

Putting it All Together

Combining the CSV line generation and HTTP header, you can create a function for downloading CSVs:

function array_to_csv_download($array, $filename = "export.csv", $delimiter = ";") {
    // Open a memory file for efficient handling without temporary files.
    $f = fopen('php://memory', 'w'); 
    // Generate CSV lines and write them to the memory file.
    foreach ($array as $line) { 
        fputcsv($f, $line, $delimiter); 
    }
    // Reset the file pointer to begin sending the file.
    fseek($f, 0);
    // Set CSV-related HTTP headers.
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="'. $filename . '";');
    // Output the generated CSV lines to the browser for download.
    fpassthru($f);
}
Copy after login

Usage Example

To use this function, simply pass your array and desired filename:

array_to_csv_download(array(
  array(1, 2, 3, 4), // First row
  array(1, 2, 3, 4) // Second row
), "numbers.csv");
Copy after login

Alternative Output Option

As an alternative to writing to a memory file, you can also use php://output as the file descriptor, eliminating the need for file seeking:

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

    // Open the php://output stream for direct output.
    $f = fopen('php://output', 'w');

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

The above is the detailed content of How to Programmatically Download CSV Files 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template