Reputation: 55
i am trying to show an image in a controller. it is now showing the image. this is what i tried to do.
1- Hardcode relative image path so that no confusion about the image
2- i put the piece of code in the constructor and it is working before "parent::__construct();
" line. but it is not showing after this line.
class Fileupload extends Frontend_Controller
{
function __construct()
{
//code is working here and showing image
parent::__construct();
//code below this line is not working
$this->load->model('front_end/user_model');
$dst_path = "C:\\xampp\htdocs\myweb\assets/uploads/avatar.jpg";
header('Content-Type: image/jpeg');
header('Content-Length: ' . filesize($dst_path));
readfile($dst_path,true);
die;
}
any suggestion??
Upvotes: 0
Views: 804
Reputation: 2487
it look like you have some output in constructor. that is disturbing the code some space or something else. remove that space from constructor "Frontend_Controller" or use ob_clean();
before your code so that it
class Fileupload extends Frontend_Controller
{
function __construct()
{
parent::__construct();
ob_clean();// that will clean your browser.
$this->load->model('front_end/user_model');
$dst_path = "C:\\xampp\htdocs\myweb\assets/uploads/avatar.jpg";
header('Content-Type: image/jpeg');
header('Content-Length: ' . filesize($dst_path));
readfile($dst_path,true);
die;
}
Upvotes: 0
Reputation: 1681
Please try this :
function __construct()
{
//code is working here and showing image
parent::__construct();
//code below this line is not working
$this->load->model('front_end/user_model');
$dst_path = "C:/xampp/htdocs/myweb/assets/uploads/avatar.jpg";
$data = file_get_contents($dst_path);
$img = imagecreatefromstring($data);
header("Content-Type: image/jpeg");
imagejpeg($img);
// die;
}
Or, please read this for more examples. :)
Upvotes: 0
Reputation: 431
Write your code like this
<?php
class Fileupload extends Frontend_Controller
{
function __construct()
{
parent::__construct();
}
public function index() {
$this->load->model('front_end/user_model');
$dst_path = "C:\\xampp\htdocs\myweb\assets/uploads/avatar.jpg";
header('Content-Type: image/jpeg');
header('Content-Length: ' . filesize($dst_path));
readfile($dst_path,true);
}
}
?> The index function is always called by default in any MVC framework. This should help. Do try and tell
Upvotes: 1