文件上传问题

时间:2018-07-12 09:00:39

标签: php codeigniter

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

我无法将文件上传到指定位置(C:// test)。var_dump($ name_file)显示上传的文件名。 $ this-> upload-> do_upload($ name_file)返回false,因此var_dump($ error)显示error message "error" => "<p>You did not select a file to upload.</p><p>You did not select a file to upload.</p>""

任何有关将文件上传到指定位置(C:// test)的帮助。

2 个答案:

答案 0 :(得分:1)

希望这对您有帮助:

注意 :确保您的c驱动器具有test文件夹,而该文件夹又具有writable权限

您的do_upload方法应如下所示:

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

您的表单应如下所示:

<?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();
?>

更多信息:https://www.codeigniter.com/user_guide/libraries/file_uploading.html

答案 1 :(得分:0)

本教程可能会帮助您:- https://www.formget.com/codeigniter-upload-image/