【问题标题】:How can I return the filename of the image upload to insert into DB?如何返回图像上传的文件名以插入数据库?
【发布时间】:2012-09-07 14:04:02
【问题描述】:

有人知道我如何获取刚刚上传的文件的名称,然后将其传递给 index() 函数,以便将其插入到我的数据库中吗?

它是用 codeigniter 编写的,这是来自我的表单控制器的代码。

谢谢。

public function index() {

    $this->load->library('form_validation');

    $this->form_validation->set_rules('nominee', 'Nominee', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[self_nominees.email]');
    $this->form_validation->set_rules('location', 'Location', 'required');
    $this->form_validation->set_rules('category', 'Category', 'required');
    $this->form_validation->set_rules('description', 'Description', 'required');
    $this->form_validation->set_rules('imageupload', 'Image Upload', 'callback__image_upload');
    $this->form_validation->set_rules('videoupload', 'Video Upload', 'callback__video_upload');

    if ($this->form_validation->run() == FALSE)
    {
        //Load the form
        $this->template('form.php');
    }
    else
    {       
        //Run code to add into the database
        $formdata = $this->input->post(NULL, TRUE); // this prevent from XSS attacks
        $this->load->model('users');
        $this->users->self_nominate($formdata['nominee'], $formdata['email'], $formdata['location'], $formdata['category'], $formdata['description'], 'image', 'video');                                    

        //Load the thankyou page
        $this->template('thankyou.php');
    }

}

/*----------------------------------------------------------------
    Image Upload Function
----------------------------------------------------------------*/

function _image_upload()
{
      $this->load->library('upload');

        // Check if there was a file uploaded
        if (!empty($_FILES['imageupload']['name']))
        {
            // Specify configuration for File 1
            $config['upload_path'] = 'uploads/images';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '100';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';       

            // Initialize config for File 1 
            $this->upload->initialize($config);

            // Upload file 1
            if ($this->upload->do_upload('imageupload'))
            {
                $data = $this->upload->data();

                return true;
            }
            else
            {
                $imageerrors = $this->upload->display_errors();
                $this->form_validation->set_message('_image_upload', $imageerrors);

                return false;
            }

        }

}

【问题讨论】:

    标签: php database forms codeigniter function


    【解决方案1】:

    来自文档,找到 here

    $this->upload->data()
    

    这是一个帮助函数,它返回一个数组,其中包含与您上传的文件相关的所有数据。这是数组原型:

    Array
    (
        [file_name]    => mypic.jpg
        [file_type]    => image/jpeg
        [file_path]    => /path/to/your/upload/
        [full_path]    => /path/to/your/upload/jpg.jpg
        [raw_name]     => mypic
        [orig_name]    => mypic.jpg
        [client_name]  => mypic.jpg
        [file_ext]     => .jpg
        [file_size]    => 22.2
        [is_image]     => 1
        [image_width]  => 800
        [image_height] => 600
        [image_type]   => jpeg
        [image_size_str] => width="800" height="200"
    )
    

    $this->upload->data() 拥有您可能需要的有关该文件的所有信息!

    【讨论】:

    • 是的,我用过这个(不在上面的脚本中)。它工作得很好,我假设这就是我获取数据的方式。但我的问题是。如何将它从 _image_upload() 函数传递回 index() 以便我可以插入它?
    • 我很困惑。 image_upload() 函数在哪里?是模型吗?如果是模型,只需 return 值并让控制器像这样获取它:$new_file = $this->your_model->image_upload();
    • 请注意,文件上传通常由控制器处理,在这种情况下,使用 CodeIgniter 的 $this->session 类传输文件名。
    • image_upload() 也在控制器中。并且 video_upload() 低于它,我只是没有将它包含在上面的代码中,因为它是相同的。我试过这样的事情: $data = array('upload_data' => $this->upload->data()); $image_name = $data['upload_data']['file_name'];只是得到未定义的变量错误。
    • 应该可以...但是你确定你正在执行$this->upload->do_upload()吗?如果变量不存在,则上传可能没有发生。 $this->upload->display_errors(); 应该可以帮助您找出答案。
    猜你喜欢
    • 2015-09-10
    • 2017-10-07
    • 1970-01-01
    • 2013-07-29
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    相关资源
    最近更新 更多