Codeigniter中的文件上传

时间:2014-11-21 07:07:08

标签: php codeigniter

尝试通过Codeigniter上传图片,但它给了我一个错误:

  

您没有选择要上传的文件。

控制器:

function register()
    {
        $this->load->view('register');
        if(isset($_POST['btnRegister']))
        {
            if($_POST['txtupass']===$_POST['txtucpass'])
            {
                //$data = array('uname'=>$_POST['txtuname'], 'uemail'=>$_POST['txtuemail'], 'upass'=>$_POST['txtupass'], 'utype'=>'user', 'uphoto'=>$_FILES['txtPhoto']['name']);
                //$this->user->register_user($data);
                //----Upload----//
                $fname = $_FILES['txtPhoto']['name'];
                $config['upload_path'] = './uploads/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '1024';
                $config['max_width']  = '1024';
                $config['max_height']  = '768';
                $this->load->library('upload', $config);
                //$FT = $_FILES['txtPhoto']['type'];
                //$Ex =substr($FT, 6, strlen($FT));
                //$FNAME = $_FILES['txtPhoto']['name'];
                //$TempName = $_FILES['txtPhoto']['tmp_name'];
                //move_uploaded_file($FNAME, './uploads/'.$FNAME.$Ex);
                //-------------//
                if ( ! $this->upload->do_upload()) { echo $this->upload->display_errors();  }
                else
                {
                    $this->upload->initialize($config);
                    $this->upload->do_upload($fname);
                    //$this->upload->data();
                    echo("<p>'".$_POST['txtuname']."' Registered Successfully!</b>");
                }
                echo("<p>'".$_POST['txtuname']."' Registered Successfully!</b>");
            }
            else
            {
                echo("<p style='color:red;'>Error: Password not match</p>");
            }
        }

    }

查看:

<body>
    <h1 align="center">Registration User</h1>
    <?php echo form_open_multipart('users/register');?>
    <table border="1" align="center">
        <tr>
            <th align="left" style="color:white;">Username</th>
            <td><input type="text" name="txtuname" style="width:270px" /></td>
        </tr>
        <tr>
            <th align="left" style="color:white;">Email</th>
            <td><input type="text" name="txtuemail" style="width:270px" /></td>
        </tr>
        <tr>
            <th align="left" style="color:white;">Password</th>
            <td><input type="password" name="txtupass" style="width:270px" /></td>
        </tr>
        <tr>
            <th align="left" style="color:white;">Confirm Password</th>
            <td><input type="password" name="txtucpass" style="width:270px" /></td>
        </tr>
        <tr>
            <th align="left" style="color:white;">Select Photo</th>
            <td><input type="file" name="txtPhoto" style="color:white;" /></td>
        </tr>
        <tr>
            <td colspan="2" align="right"><input type="submit" name="btnRegister" value="Register" /></td>
        </tr>
    </table>
    </form>
</body>

我在Stackoverflow上阅读了很多帖子并应用了所有但是得到相同的错误。我很困惑这是什么问题。

4 个答案:

答案 0 :(得分:1)

加载上传库后你必须进行初始化:

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

在您的代码中进行以下更改:

1删除此行

$fname = $_FILES['txtPhoto']['name'];

2在以下代码中进行更改。

if (!$this->upload->do_upload('txtPhoto')) {
    echo $this->upload->display_errors();
} else {
    print_r($this->upload->data());
}

它可以在我的电脑上运行。

答案 1 :(得分:1)

你的控制器功能应该是 -

function register(){
    $this->load->view('register');
    if(isset($_POST['btnRegister']))
    { 
        if($_POST['txtupass']===$_POST['txtucpass'])
        {
            $fname = $_FILES['txtPhoto']['name'];
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '1024';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';
            $this->load->library('upload', $config);
            $this->upload->initialize($config);                

            if ( ! $this->upload->do_upload('txtPhoto')) { 
                print_r($this->upload->display_errors());                    
            }
            else
            {                    
                $data = array('upload_data' => $this->upload->data());
                print_r($data);
                echo("<p>'".$_POST['txtuname']."' Registered Successfully!</b>");
            }
            echo("<p>'".$_POST['txtuname']."' Registered Successfully!</b>");
        }
        else
        {
            echo("<p style='color:red;'>Error: Password not match</p>");
        }
    }

}

此外,请检查您的上传路径,因为我可以看到错误仅在上传路径中。你的申请中存在什么上传目录?

答案 2 :(得分:0)

试试这个:

$this->upload->do_upload("txtPhoto"); 

点击此处查看reference

答案 3 :(得分:-1)

function insert()
{  
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '1000';
    $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('uploadv', $error);
    } else {
        $data = $this->upload->data();
        $iname = $data['file_name'];

        $name = $this->input->post('name');
        $age = $this->input->post('age');
        $this->Exam_mod->insert($name, $age, $img);
    }
}