Special Offer - Get $90 Discount and Host Your Dream Web Site @ Dreamhost for 1 Year Click Here. Promo Code SLJ345
Back To Top
WordPress Web Application Development
Develop Powerfull Web Applications Using Cutting Edge WordPress Development Techniques

Fixing Image Downloading Issue With Codeigniter Download Helper

on 2011/09/7 6:44 AM stored in: Uncategorized and tagged:

Introduction To Codeigniter Download Helper


About Download Helper
Codeigniter download helper is a single function helper which allows you to download files directly to your computer. This helper contains a fuction called force_download which will download any type of file.
Using Codeigniter Download Helper

        $this->load->helper('download');
        $image_name = "mypic.jpg';
        $data = file_get_contents($image_path); // Read the file's contents

        force_download($image_name, $data);


Fixing Image Download Issues In Codeigniter Download Helper


I have been working with download helper lately and i have faced some issues in downloading image file type. when you use the above code images are getting downloaded without any problem. But once downloaded and opening the image there is a error message “Error interpreting JPEG image file (Not a JPEG file: starts with 0x)”. So i have tried many things and found the following code to be usefull in fixing downloading image issue. Open the codeigniter download helper and insert the ob_clean() function before the exit($data) method which will clean any output previously sent to the browser .

ob_clean();
exit($data);

Alternative Method For Downloading Files
Instead of using codeigniter download helper, use the following code inside your controller code.

$image_name = "mypic.jpg";
$image_path = $this->config->item('base_url') . "uploads_dir/$image_name";
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=$image_name");
ob_clean();
flush();
readfile($image_path);

Leave a Reply

Follow Us On Facebook