1
votes

//views/myproject/testupload.php

  <a class="btn pull-right btn btn-primary"
       href="<?= site_url("myproject/upload/") ?>"><?= lang('upload') ?></a>

//views/myproject/upload.php

<?php echo form_open_multipart(site_url('myproject/do_upload'));?>
<?=form_line('', form_upload('userfile', $this->form_validation->set_value('userfile')));?>
<?=form_submit('upload', lang('action_upload'), 'class="btn btn-primary"');?>
<?php echo form_close();?>

//Controller/myproject/test.php

public function upload()
{
    $this->output->view('analytics/kysim/upload');
}

public function do_upload()
{
    $config['upload_path']          = 'C:/test/';
    $config['allowed_types']        = 'txt';
    $this->load->library('upload', $config);
    $name_file = $_FILES['userfile']['name'];

    if ( ! $this->upload->do_upload($name_file))
    {

        $error = array('error' => $this->upload->display_errors());
        var_dump($error);
        die();
        //$this->load->view('upload', $error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());
        var_dump($data);
        die();
        //$this->load->view('success', $data);
    }
}

I am not able to upload a file to a specified location(C://test).var_dump($name_file) displays the uploaded file name. $this->upload->do_upload($name_file) returns false and so var_dump($error) displays the error message "error" => "<p>You did not select a file to upload.</p><p>You did not select a file to upload.</p>""

Any help on uploading the file to specified location(C://test) would be appreciated.

2
$config['upload_path'] = 'C:\test\' should be. - Sanjay Kumar
@Sanjay that doesnt work - Programmer
Does Apache have access to C:/test I would doubt it. Try uploading to a folder in your document root directory, does that work? - RiggsFolly
Although "<p>You did not select a file to upload.</p> sounds like maybe you file input name is wrong - RiggsFolly

2 Answers

1
votes

Hope this will help you :

Note : make sure your c drive has test folder which in turn have writable permission

Your do_upload method should be like this :

public function do_upload()
{
    $config['upload_path']  = 'C:\test\\';
    $config['allowed_types'] = 'txt';
    $this->load->library('upload', $config);
    if (! empty($_FILES['userfile']['name']))
    {
        if ( ! $this->upload->do_upload('userfile'))
        {
            $error = array('error' => $this->upload->display_errors());
            var_dump($error);
            die();
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
            var_dump($data);
            die();
        }
    } 
}

Your form should be like this :

<?php 
  echo form_open_multipart('myproject/do_upload');
  echo form_line('', form_upload('userfile', $this->form_validation->set_value('userfile')));
  echo form_submit('upload', lang('action_upload'), 'class="btn btn-primary"');
  echo form_close();
?>

For more : https://www.codeigniter.com/user_guide/libraries/file_uploading.html