cerberus478 Posted July 24, 2012 Share Posted July 24, 2012 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']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/266183-image-view-problem/ Share on other sites More sharing options...
Mahngiel Posted July 24, 2012 Share Posted July 24, 2012 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) Quote Link to comment https://forums.phpfreaks.com/topic/266183-image-view-problem/#findComment-1364101 Share on other sites More sharing options...
cerberus478 Posted July 24, 2012 Author Share Posted July 24, 2012 Then is there a way to store the path of the image on the database? Quote Link to comment https://forums.phpfreaks.com/topic/266183-image-view-problem/#findComment-1364128 Share on other sites More sharing options...
Mahngiel Posted July 25, 2012 Share Posted July 25, 2012 of course there is. it's just letters that make words, which are just strings, right? Quote Link to comment https://forums.phpfreaks.com/topic/266183-image-view-problem/#findComment-1364155 Share on other sites More sharing options...
cerberus478 Posted July 25, 2012 Author Share Posted July 25, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/266183-image-view-problem/#findComment-1364201 Share on other sites More sharing options...
Mahngiel Posted July 25, 2012 Share Posted July 25, 2012 but I'm not sure on how to put the folder path into the database. You tell it to. You've already got $config['upload_path'] = './uploads/', and $this->upload->data(); contains the file name. Quote Link to comment https://forums.phpfreaks.com/topic/266183-image-view-problem/#findComment-1364233 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.