Welcome Guest, Not a member yet? Register   Sign In
Image upload and database storage...
#1

[eluser]HSKrustofsky[/eluser]
The site I'm building is to where the person fills out a form, and the info is stored n a database, and is uploaded to a section of the site. I have it to where text is able to be uploaded, but don't know what to do about images. I've been reading around, and from what I have seen, it is looked down upon to store images onto a database. If I want to call back that a certain image with an ID, how would I be able to do that?

Any suggestions? Thanks in advanced.
#2

[eluser]bretticus[/eluser]
It's probably not as bad as ppl say but the alternative way to do this is to give the image a distinct name and store that name in the database (I suppose if your database is on another network host, it's not a great idea afterall.) This can be done by changing the filename before storing on disk or you can programatically load an image file (map a GUID to an image path in the database) and have a PHP script write the image data to buffer.
#3

[eluser]NateL[/eluser]
Your best bet is to store the name of the file in the database, and then storing the image file in a sub directory (/images for example)

And then your controllers would access models, which access the database - retrieving the file name. The file name then gets passed onto the view file, and is echo'd out into a html img tag
#4

[eluser]HSKrustofsky[/eluser]
Thanks for the responses. My computer has been out of commission and I was unable to read anything, or try anything until now. I am a bit confused as to how to do this, or even where to start. I have a form currently where I can upload text (being read off the model and controller) but don't know how to incorporate the file upload portion of it in the view, model, and controller. I was wondering if I put my MVC up, can someone guide me in the direction of how to achieve my goal?

Thanks, and sorry for the late response.
#5

[eluser]HSKrustofsky[/eluser]
Im in dire need of some assistance. I have been looking around and came up with something I though would work, but nope, blank screen. Any ideas?

Controller:
Code:
<?php

class Admin extends Controller {
    
    function Admin() {
        parent::Controller();
    }
    
    function index() {
    
        $data = array();
    
        if($query = $this->admin_model->retrieve()) {
            $data['records'] = $query;
        }
        
        $data['title'] = "Admin";
        $this->load->view('panelview', $data);

    }

    function create() {

        if ($_POST) {
            $save = array(
                'title'  => $this->input->post('title'),
                'contents'  => $this->input->post('contents'),
                
            $config = array (
                    'upload_path' => 'uploads',
                    'allowed_types'=> 'gif|jpg|jpeg|png',
                    'max_size' => '5120';
                    'overwrite' => FALSE
                );

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

            if ($this->upload->do_upload('userfile')) {
                $data = $this->upload->data();
            }

            $file_name = !empty($data['upload_data']) ? $data['upload_data']['file_name'] : NULL ;
            
            );
            $this->admin_model->create($save, $config);
        }
        
        $data['title'] = "Add News";
        $this->load->view('addnews', $data);
        
        if($_POST != NULL) {
            redirect('../admin/');
        }

    }

    function delete() {
        
        $this->admin_model->delete();
        $this->index();

    }

    function update($id) {
        
        if ($_POST) {
            $save = array(
                'title'  => $this->input->post('title'),
                'contents'  => $this->input->post('contents'),
            );
            $this->admin_model->update($save, $id);
        }
        
        $data['news'] = $this->admin_model->get($id);
        $data['title'] = "Update";
        $this->load->view('editnews', $data);

        if($_POST != NULL) {
            redirect('../admin/');
        }
        
    }

}

Don't know if or what I need in the model and/or views. Any assistance would be awesome!




Theme © iAndrew 2016 - Forum software by © MyBB