Welcome Guest, Not a member yet? Register   Sign In
Downloading file in php using codeigniter download-helper
#1

[eluser]Unknown[/eluser]
Code:
First I have the following folder structure:

BookShop     ----> Project Name
  CodeIgniter
       images

Now inside the 'images' folder I have a pic file named rr.jpg.

I have the following controller class 'UserController' where I have written the following function :

<?php
    class UserController extends CI_Controller
    {
        public function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->helper('captcha');
            $this->load->helper('html');
            $this->load->library('form_validation');
            $this->load->library('cart');
            $this->load->helper('download');
            $this->load->model('UserModel','um',TRUE);
        }
        public function test()
        {
            $this->load->view('testing');
        }
?>

In the views I have written the following within a php webpage which I named 'testing.php' :

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        $data = file_get_contents(base_url().'/images/rr.jpg'); // Read the file's contents
        $name = 'rr.jpg';
        force_download($name, $data);
        ?>
    </body>
</html>

Now in the routes which is within the config folder I have written the following :

$route['tmp']="UserController/test";

Now when I am executing the file,the file is getting downloaded but its not showing the picture within the file.When I am opening it with Fax-Viewer its saying "No preview available" why?.The above code in views, I got from internet.

If anybody can help....thank u. Sandip Ray.
#2

[eluser]adamck[/eluser]
Have you tried the path

'../images/rr.jpg'

Instead of using a full path (remove the base_url().)

I dont think file_get_contents likes to get files that are not local (even though it is, the link looks external)

Worth a try!
#3

[eluser]Techno Heart[/eluser]
Remove base_url()

Just try with

Code:
$data = file_get_contents('images/rr.jpg'); // Read the file's contents
        $name = 'rr.jpg';
        force_download($name, $data);




Theme © iAndrew 2016 - Forum software by © MyBB