CodeIgniter - 图片上传

时间:2014-04-22 10:17:09

标签: php

每当我从浏览和点击上传中选择文件时,它都不起作用。

这是codeignitor项目。我是php和codeignitor的新手。

#My coctroller name is member and code is below:
public function profile_change_avater(){

    $this->load->model('member_model');

    if($this->input->post('upload'))
    {
        $this->member_model->do_upload();
    }
    $data['title'] = "Choise your Avater";
    echo "<pre>";
    print_r($data['title']);
    echo "</pre>";
$this->common_load_view('member/user_extended_profile_change_avater_content_view',
'members', 'profile_change_avater', $data );

}

My model name is member_model, code is below:

public function do_upload(){

    //$this->image_path = realpath(APPPATH .'../asset/images' );
    $this->upload->initialize($config);
    $config = array(
        'allowed_type' => 'jpg|jpeg|gif|png',
        'upload_path'  => 'image_path',     
        'max_size'     => '2000'
    );

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

    if( ! $this->upload->do_upload()){
        $error = array('error'=>$this->upload->display_error());
        return $error;
    }
    else 
    {
        $data = array('upload_data'=> $this->upload->data());
        return $data;
    }


}
 Here is the code view:
<p id="avatar-upload">
<?php   echo  form_open_multipart('member/profile_change_avater');
    echo form_upload('userfile');
    echo form_submit('upload','Upload');
     echo form_close();
     ?>
</p>

我的代码出了什么问题。如果有任何身体帮助我。它世界很棒。

2 个答案:

答案 0 :(得分:1)

在视图中

<p id="avatar-upload">
<?php   echo  form_open_multipart('member/profile_change_avater'); ?>
<input type="file" name='image'>
<?php    echo form_submit('upload','Upload');
     echo form_close();
     ?>
</p>

并更改profile_change_avater()的条件

if($this->input->post())
    {
        $config = array(

            'allowed_type' => 'jpg|jpeg|gif|png',
            'upload_path'  => 'image_path',     
            'max_size'     => '2000'
        );

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


        if( ! $this->upload->do_upload('image')){
            $error = array('error'=>$this->upload->display_error());

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

        }

    }

答案 1 :(得分:0)

控制器

public function uesr_profile(){     
    if($this->input->post()){
        $config['tmp_name'] = $_FILES['userfile']['tmp_name'];
        $config['upload_path'] = './';
        $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
        $config['max_size'] = 1024 * 5;
        $config['max_width']  = 1024;
        $config['max_height']  = 768;   
        $config['file_name'] = $_FILES['userfile']['name'];

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

        if (!$this->upload->do_upload()){
            $this->data['error'] =  $this->upload->display_errors();
        }
    }      
        $this->load->view('profile_page',$this->data);              
}

查看

<?php echo form_open_multipart('users/uesr_profile'); ?>
<input type="file" name='userfile'>
<?php  echo form_submit('upload','Upload');
echo form_close(); ?>