简体   繁体   中英

File Uploading in Codeigniter

Trying to Upload image through Codeigniter but its gives me an error:

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

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

2 Make changes in your following code.

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

It's works on my 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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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