Special Offer - Get $90 Discount and Host Your Dream Web Site @ Dreamhost for 1 Year Click Here. Promo Code SLJ345
Back To Top
Building Impressive Presentations with impress.js
Are you tired of creating boring Presentations?
Take a look at the the future of online presentations with impress.js
in my latest book

Fixing Image Downloading Issue With Codeigniter Download Helper

on 2011/09/7 6:44 AM stored in: Codeigniter, My Experiences 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);

You may have browsed many websites and spent hours to find the informaion provided in this tutorial. So take 1 minute to share this post to help others find it quickly. Thank You for visiting InnovativePHP.

 

Popular Posts
Recent Posts

 

Comments are closed.

Follow Us On Facebook

Page optimized by WP Minify WordPress Plugin