Magento custom options files or images corrupt download


What is happening?

Often store owners need to create custom options at product level to upload files/images, these file upload options appear on product detail page so customers can upload their files.

For example there is a print site, customers pick the business card and want to upload their own design (front and back). For this purpose product is configured at backend with ‘file’ custom options, so customers can upload their own files. Customers choose their own design and upload design with order and checkout.

You as Store owner go to backend to check order detail and try to download custom option files uploaded by customer.
You may experience that the custom options files downloaded are corrupt, these are not opening by any application, but when you try to download them directly using ftp these are ok and opened correctly.

Where is the problem?

When downloading file, Magento calls the _prepareDownloadResponse function defined in the
/app/code/core/Mage/Core/Controller/Front/Action.php file.

Before sending the file contents to output buffer magento does clear the message body but it DOESN’T clear the output buffer, so any thing printed or echoed before download process is also included in the output buffer, as a result when file is downloaded it contains the space or other characters in start and when download it fails to open correctly.

How to fix that?

Fix is very simple. We just need to clear the output buffer before Magento starts sending headers, so you just need to put an ob_clean() function before sending file headers in download process.
Open the following file
/app/code/core/Mage/Core/Controller/Front/Action.php
and in function
_prepareDownloadResponse
Look for the line where it is sending file header i.e
$this->getResponse()->sendHeaders();
and put ob_clean(); right before sending headers.

It should look like this

protected function _prepareDownloadResponse($fileName, $content, $contentType = 'application/octet-stream',
        $contentLength = null
    ) {
    ...
      $this->getResponse()->clearBody();
      ob_clean();
      $this->getResponse()->sendHeaders();
    ...

Save the file and now try to download the file, it will now download correctly.

Wasn’t that awesome?

Advertisements

2 thoughts on “Magento custom options files or images corrupt download

  1. Mark Smith says:

    Downloading the file works during the checkout process but in My Account or Admin I get a 404 error. Do you have any idea why this happens? I have a feeling it has something do with cookies or sessions but I’m not sure.

  2. Tofeeq says:

    Most likely a permissions issue, but it may be related to new extension installation after which you need to refresh the resources and permissions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s