0

Hello all im working on a admin system that can create news with a image but i cant find out how to send the image name from my model file to my controller,

this is my model file:

function uploadImg()
{
    $config = array(
        'allowed_types' => 'jpg|jpeg|gif|png',
        'upload_path' => $this->gallery_path,
        'max_size' => 2000,
        'encrypt_name' => true
    );

    $this->load->library('upload', $config);
    $this->upload->do_upload();
    $image_data = $this->upload->data();

    $config = array(
        'source_image'    => $image_data['full_path'],
        'new_image'       => $this->gallery_path . '/thumbs',
        'maintain_ration' => true,
        'width'           => 200,
        'height'          => 200,
        'encrypt_name'    => true,
        'max_size'        => 2000
    );

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


    # Ret profil billed navn #

    $file_array = $this->upload->data('file_name');
    return $billed_sti['billed_sti'] = $file_array['file_name'];

    //$this->db->where('username', $this->input->post('username'));
    //$this->db->update('users', $profilBilledNavn);
}

This is my controller:

function opret() {

    $this->form_validation->set_rules('overskrift', 'overskrift', 'required');
    $this->form_validation->set_rules('description', 'description', 'required');
    $this->form_validation->set_rules('indhold', 'indhold', 'required');

    if($this->form_validation->run() == true)
    {

        $this->load->model('admin/nyheder_model');
        $billed_sti = $this->nyheder_model->uploadImg();

        $data = array(
            'overskrift'  => $this->input->post('overskrift'),
            'description' => $this->input->post('description'),
            'indhold'     => $this->input->post('indhold'),
            'billed_sti'  => $billed_sti,
            'brugernavn'  => $this->session->userdata('username'),
            'godkendt'    => 'ja'
        );

        $this->db->insert('nyheder', $data); 

        redirect('admin/nyheder/index');
    } else {
        $this->index();
    }


}
1
  • 1
    Already mentioned by Alex, you shouldn't put that in your model - it should be in your controller. Commented Feb 14, 2011 at 13:14

3 Answers 3

1

I do the image processing in the controller rather than the model.

"Models are PHP classes that are designed to work with information in your database."

from: http://codeigniter.com/user_guide/general/models.html

Sign up to request clarification or add additional context in comments.

Comments

1

What you need to do is move the code for uploading the image to the controler.

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);
        }
    }

Once you did that,

You can insert the name of the file from the $data variable created in this line:

$data = array('upload_data' => $this->upload->data());

and you can get the value like this:

$data['file_name']

The file will upload the the folder you configured, and you will insert the filename to the DB From the controller.

I hope it helps.

Comments

0

Please use the upload function in your controller as the model classes are used to handle the database information. Please check the code below

//Controller Class

function upload_image()
{
//Check for the submit
// Submit Name refers to the name attribute on the submit input tag.
// $filename refers to the name attribute of the file input tag.

if($_SERVER['REQUEST_METHOD'] == "POST")
{
$submit = $this->input->post('submit');
if($submit == "Submit Name")
{
//Load the relevant classes and libraries
$this->load->library('upload');
$this->load->model('admin/nyheder_model','nmodel');
$filename = "image_file";
//Define the config array
$config = array();
$config['upload_path'] = $this->gallery_path;
$config['allowed_types'] = "jpg|gif|png";
$config['max_size'] = 0; //0 is for no limit

$this->upload->initalize($config);

if(!$this->upload->do_upload("$filename"))
{
echo $this->upload->display_errors();
}
else
{
$file_data = $this->upload->data();
$filename_1 = $file_data['file_name'];
$insert_array = array('filename'=>"$filename_1");
$this->nmodel->insert_data($insert_array);
} // end of the else statement
} // end of the isset statement
} // end of the outer conditional statement

Now you have the value of the filename in the $filename_1 variable which you can pass to the model class and can store the value in the database.

Thanks J

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.