Jump to content

image view problem


cerberus478

Recommended Posts

Hi

 

My problem is that I want to view an image, but when I view it I get a bunch of gibberish.

 

I'm using codigniter.

I set my image column as a BLOB.

 

This is my bras controller

<?php
class Bras extends CI_Controller {

public function __construct()
{
	parent::__construct();
	$this->load->model('bras_model');
}

public function index()
{
	$data['bras'] = $this->bras_model->get_bras();
	$data['title'] = 'bras archive';

$this->load->view('templates/header', $data);
$this->load->view('bras/index', $data);
$this->load->view('templates/footer');
}

public function view($slug)
{
$data['bras_item'] = $this->bras_model->get_bras($slug);

if (empty($data['bras_item']))
{
	show_404();
}

$data['title'] = $data['bras_item']['title'];

$this->load->view('templates/header', $data);
$this->load->view('bras/view', $data);
$this->load->view('templates/footer');
}

public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');

$data['title'] = 'Create a bra item';

$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('content', 'content', 'required');

if ($this->form_validation->run() === FALSE)
{
	$this->load->view('templates/header', $data);	
	$this->load->view('bras/create');
	$this->load->view('templates/footer');

}
else
{
	$this->bras_model->set_bras();
	$this->load->view('bras/success');
}
}
}

 

This is my bras_model

<?php
class Bras_model extends CI_Model {

public function __construct()
{
	$this->load->database();
}

public function get_bras($slug = FALSE)
{
if ($slug === FALSE)
{
	$query = $this->db->get('bras');
	return $query->result_array();
}

$query = $this->db->get_where('bras', array('slug' => $slug));
return $query->row_array();
}

public function set_bras()
{
$this->load->helper('url');

$slug = url_title($this->input->post('title'), 'dash', TRUE);

$data = array(
	'title' => $this->input->post('title'),
	'slug' => $slug,
	'content' => $this->input->post('content')
);

return $this->db->insert('bras', $data);
}
}

 

This is my view

<?php
echo '<h2>'.$bras_item['title'].'</h2>';
echo $bras_item['image'];
echo $bras_item['content'];

?>

Link to comment
Share on other sites

Question: Why are you storing the image in the database? That's unnecessary bloat.

 

This 'gibberish' you are viewing are the binary forms of the images.  You could store the images in a folder and recall the images from there (preferred) - or you'll need to set headers (ill advised)

Link to comment
Share on other sites

I've maneged to get the pics to uplaod into a folder on the server, but I'm not sure on how to put the folder path into the database.

 

This is my upload controller

<?php

class Upload extends CI_Controller {

function __construct()
{
	parent::__construct();
	$this->load->helper(array('form', 'url'));

}

function index()
{
	$this->load->view('upload_form', array('error' => ' ' ));


}

function do_upload()
{
	$config['upload_path'] = './uploads/';
	$config['allowed_types'] = 'gif|jpg|png';
	$config['max_size']	= '100';
	$config['max_width']  = '1024';
	$config['max_height']  = '768';

	$this->load->library('upload', $config);

	if ( ! $this->upload->do_upload())
	{
		$error = array('error' => $this->upload->display_errors());

		$this->load->view('upload_form', $error);
	}
	else
	{
		$data = array('upload_data' => $this->upload->data());

		$this->load->view('upload_success', $data);
	}
}
}
?>

 

and this is the upload_form

<html>
<head>
<title>Upload Form</title>
</head>
<body>

<?php echo $error;?>

<?php echo form_open_multipart('upload/do_upload');?>

<input type="file" name="userfile" size="20" />

<br /><br />

<input type="submit" value="upload" />

</form>

</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.