使用Code Igniter上传图像?

时间:2012-09-05 22:44:33

标签: codeigniter file-upload

我正在尝试设置网站,以便我可以使用代码点火器(CI)中的网络表单上传图像。我没有收到任何错误,但文件也没有被保存。我想知道那些成功上传图片的人是否有助于解释可能出现的问题?

View:
<?php
    echo form_open_multipart('admin/galleryUpload') . "\n";
    echo "<div class='span-8'><span class='text'>Image:</span>" . form_upload('uploadImg') . "</div>";
    foreach ($gallery as $picture)
    {
        $order[] = $picture->order;
    }
    $order[] = count($order) + 1;
    echo "<div class='span-6 last'><span>Image Order #:</span>" . form_dropdown('order', $order) . "</div><div class='span-14'>&nbsp;</div>";
    $conf = array('name' => 'alt_text', 'size' => '75');
    echo "<div class='span-14 last'><span>Discription:</span>" . form_input($conf) . "<br /></div>";
    echo form_hidden('propertyID', "$propertyID");
    echo form_submit('upload', 'Upload');
    echo form_close();
?>

Controller:
class Admin extends Controller
{
    function galleryUpload()
    {
        if (! $this->session->userdata('is_admin'))
        {
            redirect('admin/index');
        }
        else
        {
            $this->load->model('admin_model');
            $this->admin_model->imgUpload();
        }
    }
}

Model:
class Admin_model extends Model
{
    function imgUpload()
    {
        $id = $this->input->post('propertyID');
        $order = $this->input->post('order');
        $alt_text = $this->input->post('alt_text');

        $config = array(
                'allowed_types' => 'jpg|jpeg|gif|png',
                //'upload_path' => '../' . $this->imgPath($id),
                'upload_path' => '../img/galleries/temp/',
                'max_size' => '5000', // 5MB files max
                );

        $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->imgPath($id) . '/thumbs',
                'maintain_ratio' => TRUE,
                'width' => '60'
                );
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();
    }
}

1 个答案:

答案 0 :(得分:1)

来自user guide

  

默认情况下,上传例程期望文件来自表单   字段名为userfile,表单必须是多部分类型

因此,您必须将表单字段的名称更改为userfile

form_upload('userfile')

将表单字段的名称传递给do_upload

$this->upload->do_upload('uploadImg');