【问题标题】:codeigniter upload imagecodeigniter 上传图片
【发布时间】:2011-02-13 14:52:27
【问题描述】:

大家好,我正在使用可以使用图像创建新闻的管理系统,但我不知道如何将图像名称从我的模型文件发送到我的控制器,

这是我的模型文件:

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

这是我的控制器:

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


}

【问题讨论】:

  • Alex 已经提到过,你不应该把它放在你的模型中——它应该放在你的控制器中。

标签: php codeigniter model controller


【解决方案1】:

我在控制器而不是模型中进行图像处理。

“模型是 PHP 类,旨在处理数据库中的信息。”

来自:http://codeigniter.com/user_guide/general/models.html

【讨论】:

    【解决方案2】:

    您需要做的是将上传图片的代码移动到控制器中。

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

    一旦你这样做了,

    您可以从该行中创建的 $data 变量中插入文件名:

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

    你可以得到这样的值:

    $data['file_name']

    该文件将上传您配置的文件夹,您将从控制器将文件名插入数据库。

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      请使用控制器中的上传功能,因为模型类用于处理数据库信息。请检查下面的代码

      //控制器类

      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
      

      现在您在 $filename_1 变量中获得了文件名的值,您可以将其传递给模型类并将该值存储在数据库中。

      谢谢 J

      【讨论】:

        猜你喜欢
        • 2013-08-23
        • 2011-06-08
        • 2012-01-22
        • 1970-01-01
        • 1970-01-01
        • 2012-05-29
        • 1970-01-01
        相关资源
        最近更新 更多