简体   繁体   English

在Codeigniter中上传文件

[英]File Uploading in Codeigniter

Trying to Upload image through Codeigniter but its gives me an error: 尝试通过Codeigniter上载图片,但出现错误:

You did not select a file to upload. 您没有选择要上传的文件。

Controller: 控制器:

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>");
            }
        }

    }

View: 视图:

<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>

I have read many posts on Stackoverflow and applied all but getting same error. 我已经阅读了许多关于Stackoverflow的文章,并应用了所有文章,但都收到了相同的错误。 I am confuse what is the problem. 我混淆了问题所在。

You have to do initialize after loading upload library: 加载上传库后,您必须进行初始化:

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

Make following changes in your code: 在代码中进行以下更改:

1 Remove this line 1删除此行

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

2 Make changes in your following code. 2在您的以下代码中进行更改。

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

It's works on my PC. 它可以在我的PC上运行。

Your controller function should be - 您的控制器功能应为-

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>");
        }
    }

}

Also, Please check your upload path, as I can see the error is in upload path only. 另外,请检查您的上传路径,因为我看到的错误仅在于上传路径。 What does uploads directory exists in your application? 您的应用程序中存在什么上载目录?

Try with this: 试试这个:

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

check here for reference 检查这里供参考

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM