How to write Some Data in CSV file in Magento?

How to write Some Data in CSV file in Magento?

July 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to write Some Data in CSV file in Magento?

Writing Shipment Data to a CSV File in Magento

Magento provides robust tools for handling data exports, including to CSV format. However, creating a downloadable CSV file involves more than just saving the data—you need to handle the HTTP response correctly. Let’s dive into how you can implement this.

Understanding the Issue

The issue lies in how Magento handles file downloads. The Varien_File_Csv class simply saves the CSV data to the designated file path, but it doesn’t provide a download mechanism out of the box.

Your current implementation is almost correct—it’s correctly using the Varien File classes to create and save the CSV data. However, to make this file available for download, you need to set up the HTTP response appropriately.

Implementing the Download

To enable downloading, you’ll need to implement two key functions:

  1. $this->_prepareDownloadResponse($filename, $content, $contentType = 'application/octet-stream')

This function prepares a HTTP response that allows the file to be downloaded by the user’s browser instead of being saved directly to the server.

  1. $this->_sendUploadResponse($filename, $content, $contentType = 'application/octet-stream', $isAppend = false)

This function sends the actual download response to the client.

Here’s an example of how you can modify your code:

public function bluedartmanifestuploadAction() {
    $request = $this->getRequest();
    $shipmentIds = $request->getPost('shipment_ids', array());
    $file_path = 'manifest_upload_' . Mage::getSingleton('core/date')->date('d-m-Y_H-i-s') . '_csv.csv';
    $mage_csv = new Varien_File_Csv();

    // Prepare shipment data for CSV
    $shipment_csv = array();
    /* Putting data in $shipment_csv[] */

    // Save data to a temporary file
    $temp_file_path = tempnam(Mage::getBaseDir('var'), 'csv');
    $mage_csv->saveData($temp_file_path, $shipment_csv);

    // Prepare the download response
    $filename = 'shipment_manifest_' . date('Y-m-d_H-i-s') . '.csv';
    $this->_prepareDownloadResponse($filename, readfile($temp_file_path));

    // Delete the temporary file
    @unlink($temp_file_path);
}

In this updated code:

  • We use tempnam to create a temporary file in the var/tmp directory.
  • After saving the CSV data to the temporary file using saveData, we read the entire contents of that file using readfile.
  • We then pass these contents to the _prepareDownloadResponse function along with the desired filename. This prepares the HTTP response for a download.
  • Finally, we delete the temporary file to clean up.

Best Practices

Remember these best practices:

  1. Use unique filenames: Generating a unique filename for each export helps prevent conflicts and ensures that your downloads are always available.
  2. Handle exceptions: Always include error handling in your data export processes to avoid leaving corrupted files or causing server errors.
  3. Monitor file sizes: Large CSV exports can consume significant system resources. Monitor the size of your exported files and implement pagination if necessary.

Conclusion

Creating a downloadable CSV file in Magento requires more than just saving the data—it involves preparing the HTTP response correctly and handling the temporary file appropriately.

By implementing these steps, you should now be able to provide users with a clickable link that allows them to download their shipment manifest as a CSV file directly from your Magento website.

Don’t hesitate to explore Laravel Company’s guides for more in-depth tutorials on Magento development.

Orders Import/Export Errors CLI

Our network