【问题标题】:CodeIgniter File upload not working and not giving errorCodeIgniter 文件上传不起作用且未给出错误
【发布时间】:2016-02-26 10:20:44
【问题描述】:

我正在尝试从 CodeIgniter 上传文件。文件没有给出任何错误,甚至没有将文件存储在根目录或 codeigniter 中的上传文件夹中。有人可以帮忙吗?请注意,因为我已经向我的上传文件夹授予了 755 权限。下面是我的代码

查看文件

<html>
<head>
<title>Upload Form</title>
</head>
<body>

<?php echo $error;?>

<?php echo form_open_multipart('UploadController');?>

<input type="file" name="userfile" size="20" />

<br /><br />

<input type="submit" value="upload" />

</form>

</body>
</html>

下面是我的控制器

<?php

class UploadController extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    function index()
    {
        $this->load->view('uploadview', array('error' => ' ' ));
    }

    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("add_1"))
        {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_form', $error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());

            $this->load->view('uploadview', $data);
        }
    }
}
?>

请在这个问题上帮助我。

【问题讨论】:

    标签: codeigniter file-upload


    【解决方案1】:

    您的代码中有一些错误: 请更改以下行:

    1.改变 form_open_multipart('UploadController') form_open_multipart('UploadController/do_upload')

    2.改变 $this-&gt;upload-&gt;do_upload("add_1") $this-&gt;upload-&gt;do_upload("userfile")。 3.改变 $this-&gt;load-&gt;view('upload_form', $error);
    $this-&gt;load-&gt;view('uploadview', $error);

    【讨论】:

      【解决方案2】:

      改变

      form_open_multipart('UploadController')
      

      form_open_multipart('UploadController/do_upload')
      

      【讨论】:

        【解决方案3】:

        更改表单操作路径

        form_open_multipart('UploadController/do_upload')

        或者你可以像这样在一种方法中使用它:

        function index()
        {
            $config['upload_path']      = 'upload_path';
          $config['allowed_types']    = 'gif|jpg|jpeg|png';
          $config['max_size']         = 5120;
          $config['encrypt_name']     = true;
        
          $this->load->library('upload', $config);
        
            $this->form_validation->set_rules('image', 'lang:image', 'file_size_max[5M]|file_allowed_type[image]');
        
            if($this->form_validation->run() == FALSE)
            {
                $this->load->view('uploadview', array('error' => ' ' ))
            }
            else
            {
                $user = array();
        
                $uploaded  = $this->upload->do_upload('add_1');
        
            //delete the original file if another is uploaded
            if($uploaded)
            {           
              $data = array('upload_data' => $this->upload->data());
        
              $this->load->view('uploadview', $data);
            } else {
              $error = array('error' => $this->upload->display_errors());
        
              $this->load->view('upload_form', $error);
            }
            }
        }
        

        并确保您的上传路径正确。

        【讨论】:

          猜你喜欢
          • 2015-08-05
          • 2013-03-17
          • 2013-12-09
          • 2018-02-19
          • 2016-02-20
          • 2012-03-22
          • 2017-12-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多