Leveraging In-Memory Temporary Files for CSV Exports in PHP

Leveraging In-Memory Temporary Files for CSV Exports in PHP

In web development, exporting data to CSV format is a common task. However, handling CSV exports efficiently and securely can be challenging, especially when dealing with large datasets or sensitive information. One effective solution is to use temporary files in memory, which PHP supports via the php://temp stream. This article explores the benefits and implementation of this approach.

Why Use Temporary Files in Memory?

  1. Performance: In-memory operations are faster than disk I/O.
  2. Cleanliness: No temporary files are left on the disk, reducing clutter.
  3. Security: Sensitive data stays in memory, minimizing the risk of unauthorized access.

Creating and Managing In-Memory Temporary Files

PHP provides a convenient way to create temporary files in memory using the php://temp stream. This allows you to handle file content without creating an actual file on the disk. Here’s a step-by-step guide to using this approach for CSV exports:

  1. Open a Stream: Use fopen('php://temp', 'w+') to open a temporary file for reading and writing.
  2. Write Data: Use standard file functions like fputcsv to write data to the stream.
  3. Read Data: Use rewind to reset the stream pointer and stream_get_contents to read the content.
  4. Close the Stream: Use fclose to close the stream when done.

Example: Exporting Product Data to CSV

Here’s an example of how to create a CSV file in memory and provide it as a download in a Laravel controller:

public function exportProductsToCsv()
$products = Product::groupBy(‘products.id’)->orderBy(‘unit’, ‘desc’)->get();

// Create a temporary file in memory
$fp = fopen('php://temp', 'w+');

// Write the CSV header
$header = ["Sl", "Code", "Product Name", "Last Purchase Price", "Last Selling Price"];
foreach ($new_array as $setting) {
    $header[] = $setting['name'] . " Unit";
fputcsv($fp, $header);

foreach ($products as $key => $val) {
    $code = str_replace(',', ' | ', $val->code);
    $sl = $key + 1;
    $last_purchase_price = Purchase::where('product_id', $val->id)->latest('purchase_date')->first();
    if ($last_purchase_price) {
        $last_purchase_price = number_format($last_purchase_price->purchase_price, 2, '.', '');
    } else {
        $last_purchase_price = number_format($val->price, 2, '.', '');

    $last_sale_price = number_format((Sale::where('product_id', $val->id)->latest('created_at')->first()) ? Sale::where('product_id', $val->id)->latest('created_at')->first()->unit_price : 0, 2, '.', '');

    // Collect the row data
    $row = [$sl, $code, $val->name, $last_purchase_price, $last_sale_price];

    if (Session('setting_id')) {
        $row[] = $val->unit;
    } else {
        foreach ($new_array as $setting) {
            $column_name = $setting['column_name'];
            $row[] = $val->$column_name;

    // Write the row to the CSV
    fputcsv($fp, $row);

// Reset the file pointer to the start of the file

// Capture the CSV content
$csv = stream_get_contents($fp);

// Close the file pointer

$headers = [
    'Content-Type' => 'text/csv; charset=UTF-8',
    'Content-Disposition' => 'attachment; filename="product_list.csv"',
    'Pragma' => 'public',
    'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
    'Expires' => '0',

// Add BOM for UTF-8 compatibility with Excel
echo "\xEF\xBB\xBF";
\LogActivity::addToLog(\Auth::user()->username . ' product exported file successfully.');

return response($csv, 200, $headers);



Using in-memory temporary files for CSV exports in PHP offers a robust solution for handling data efficiently and securely. By leveraging the php://temp stream, you can avoid common pitfalls associated with disk I/O and ensure that your data export process is both fast and clean. This approach is particularly useful in web applications where performance and security are paramount.

About Author

Nahid Bitspeck

Leave a Reply

Your email address will not be published. Required fields are marked *