Codeigniter文件上传错误:'您没有选择要上传的文件'

时间:2015-11-07 10:27:17

标签: php codeigniter

我正在使用codeigniter 3,在You did not select a file to upload.函数中收到类似do_upload()的错误,但正常的php函数move_uploaded_file()正常工作。我提到了stackoverflow的大多数答案,但我没有得到解决方案。

我认为它可能出现在wamp问题中,但我没有清楚地知道它在哪里。 如果这个代码在你的机器上工作,那么它将在我的wamp php或Apache设置中出现。

查看:(upload.php)

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php echo form_open_multipart('welcome/do_upload'); ?>
    <input id="sfile" type="file" name="file">
    <input type="submit" value="Submit">
<?php echo form_close(); ?>
</body>
</html> 

控制器:(welcome.php)

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()) //not working
        {
            $error = array('error' => $this->upload->display_errors());
            var_dump($error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
        }
        echo $basename = $_FILES['file']['name'];
        move_uploaded_file($_FILES['file']['tmp_name'],$config['upload_path'].$basename);
        // move_uploaded_file() works fine
    }

3 个答案:

答案 0 :(得分:1)

根据CI 3,您需要在$this->upload->do_upload('')中传递名称属性。

所以你需要在其中传递name属性

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

检查您上传的文件大小,因为您设置了$ config['max_size'] = '100';。这意味着你无法上传文件大小超过100 kb

阅读File Uploading Class

答案 1 :(得分:0)

首先加载上传库,然后初始化配置数据。

并在$ this-&gt; upload-&gt; do_upload('file')中传递输入文件类型的属性名称。

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);  # Load upload library
    $this->upload->initialize($config);   # Initialize 
    if ( ! $this->upload->do_upload('file')) 
    {
        $error = array('error' => $this->upload->display_errors());
        var_dump($error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());
    }

}

答案 2 :(得分:0)

这可能与301错误导致的职位损失有关。

尝试更换

<?php echo form_open_multipart('welcome/do_upload'); ?>

具有(解决方法)

<form action="do_upload" method="post" enctype="multipart/form-data">

如果有效,则路由配置中会出现错误