nrs jayaram
nrs jayaram

Reputation: 3438

Codeigniter File Upload error: 'You did not select a file to upload'

I am using codeigniter 3, getting error like You did not select a file to upload. in do_upload() function, but normal php function move_uploaded_file() works fine. i referred most of the answers from stackoverflow but i did not get solution.

I think it may be in wamp issue, but i did not get clearly where it is. if this code works in your machine then it will be issue in my wamp php or Apache settings.

View: (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> 

Controller: (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
    }

Upvotes: 1

Views: 7970

Answers (3)

Manuel Sansone
Manuel Sansone

Reputation: 349

It could be related to a post loss due to 301-error.

Try replacing

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

with (work around)

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

If it works, there's some error in routing configs

Upvotes: 1

Arvind Jaiswal
Arvind Jaiswal

Reputation: 442

First load upload library and then after initialize config data.

And pass attribute name of your input file type in $this->upload->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());
    }

}

Upvotes: 0

Saty
Saty

Reputation: 22532

AS per CI 3 you need to pass name attribute in your $this->upload->do_upload('').

So you need to pass name attribute in it

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

Check size of file you have uploaded because you set $config['max_size'] = '100';. It means you will not able to upload file size for more then 100 kb

Read File Uploading Class

Upvotes: 2

Related Questions