【问题标题】:File uploading error in CodeigniterCodeigniter 中的文件上传错误
【发布时间】:2016-05-19 00:34:11
【问题描述】:

我正在尝试在 codeigniter 中上传图片。

这是我的视图文件代码。

<form action="<?php echo site_url('pages/data_submitted') ?>" method="get" enctype="multipart/form-data">
Image: <input type="file" name="image"/>
<button>Submit</button>
</form>

这是我的控制器代码。

class Pages extends CI_Controller 
{ 
 public function data_submitted(){
 $config['upload_path']   =   "img/";
$this->load->library('upload',$config);
$this->upload->do_upload();
$finfo=$this->upload->data();
$data = $this->upload->display_errors();

$this->load->model('user_model');
$this->user_model->insert_item($data);
}
}

这是我的模型代码

<?php
class User_model extends CI_Model {
function __construct(){
    /* Call the Model constructor */
    parent::__construct();
}


public function insert_item($item){

    print_r($item);
}
}
?>

这段代码有什么问题... 这里我传递了 $data 只是为了检查是否发生任何错误。 它显示“您没有选择要上传的文件。”即使我选择了一个文件。 请帮帮我。

【问题讨论】:

标签: php codeigniter codeigniter-2 codeigniter-3


【解决方案1】:

在 codeigniter 中上传文件要容易得多。在这里,我正在为视图文件和控制器文件编写代码。

查看文件

<html>
<body>
<?php if(isset($error)){echo $error;} ?>
<?php if(isset($success)){echo $success;} ?>    
<?php echo form_open_multipart('upload_controller/do_upload');?>
<input type='file' name='userfile' size='20' />
<input type='submit' name='submit' value='upload' /> 
</form>
</body>
</html>

控制器文件

为了启用文件上传,我们必须加载一个库它的“上传”

$this->load->library('upload');

<?php 

class Upload_Controller extends CI_Controller {

public function __construct() {
     parent::__construct();
}

public function index(){
    $this->load->view('fileupload_view');
}

public function do_upload(){

if (!is_dir('/upload')) {
    mkdir('./upload',777,0);         
}

$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,    
);

$this->load->library('upload', $config);

if($this->upload->do_upload())
{
     $data['upload_data'] = $this->upload->data();
     $data['success']= 'File Successfully Uploaded';
     $this->load->view('fileupload_view',$data);
}
else
{
     $error = array('error' => $this->upload->display_errors());
     $this->load->view('fileupload_view', $error);
}
}
}
?>

【讨论】:

    【解决方案2】:

    参考此代码。这肯定对你有用

    public function uploadImage() {
            $this->load->helper(array('form', 'url'));  
            $config['upload_path'] = 'assets/images/b2bcategory';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '1000';
            $config['max_width'] = '2024';
            $config['max_height'] = '1768';
            $config['width'] = 75;
            $config['height'] = 50;
            if (isset($_FILES['catimage']['name'])) {
                $filename = "-" . $_FILES['catimage']['name'];
                $config['file_name'] = substr(md5(time()), 0, 28) . $filename;
            }
            $config['overwrite'] = TRUE;
            $config['remove_spaces'] = TRUE;
            $field_name = "catimage";
            $this->load->library('upload', $config);
            if ($this->input->post('selsub')) {
                if (!$this->upload->do_upload('catimage')) {
                    //no file uploaded or failed upload
                    $error = array('error' => $this->upload->display_errors());
                } else {
                    $dat = array('upload_data' => $this->upload->data());
                    $this->resize($dat['upload_data']['full_path'],           $dat['upload_data']['file_name']);
                }
                $ip = $_SERVER['REMOTE_ADDR'];
                if (empty($dat['upload_data']['file_name'])) {
                    $catimage = '';
                } else {
                    $catimage = $dat['upload_data']['file_name'];
                }
                $data = array(            
                    'ctg_image' => $catimage,
                    'ctg_dated' => time()
                );
                $this->b2bcategory_model->form_insert($data);
    
            }
        }
    

    【讨论】:

      【解决方案3】:

      使用$this-&gt;upload-&gt;do_upload('image'); 而不是$this-&gt;upload-&gt;do_upload();

      您必须在 do_upload() 中将文件输入名称作为参数传递。如果您没有传递字段名称,则默认情况下它将采用 userfile 。这就是为什么它给出You did not select a file to upload. 错误

      【讨论】:

        【解决方案4】:

        试试这个:

        查看页面

        <?php echo echo form_open_multipart(base_url('Pages/data_submitted'),['name' => 'form', 'id' => 'form']);
        
          //or <form action="<?php echo base_url('Pages/data_submitted') ?>" method="post" enctype="multipart/form-data">
        
           Image: <input type="file" name="image"/>
           <button id="button" name="button">Submit</button>
        
        </form>
        

        控制器

        class Pages extends CI_Controller 
        { 
            public function data_submitted(){
        
                $config['upload_path'] = getcwd().'/img/';
                $config['allowed_types'] = 'jpg|png';
                $config['max_size'] = 2500;
                $config['remove_space'] = TRUE;
        
                $this->load->library('upload',$config);
                $this->load->model('user_model');
        
                if($this->upload->do_upload(image))
                {
                    $data = $this->upload->data();
                    $this->user_model->insert_item($data);
                }
                else
                {
                   $this->upload->display_errors();            
                }
             }
        }
        

        【讨论】:

          【解决方案5】:

          你必须在 do_upload 方法中提及字段名称。

          <form method="post" enctype="multipart/form-data">
              <input type="file" name="field_name"/>
          </form>
          
          <?php
           $this->upload->do_upload('field_name');
           ?>
          

          【讨论】:

            【解决方案6】:

            改变这个:

            <button>Submit</button>
            

            到这里:

            <input type="submit" value="Submit" name="submit">
            

            在控制器中

            class Pages extends CI_Controller 
            { 
                public function data_submitted(){
            
                    $this->load->helper(array('form', 'url'));
            
                    $config['upload_path']   =   "./img/";
                    $config['allowed_types'] = 'gif|jpg|png'; # Changed 
            
                    $this->load->library('upload',$config);
                    if(!$this->upload->do_upload())
                    {
                        $data = $this->upload->display_errors();
                    }
                    else{
                        $finfo = $this->upload->data();
                        $this->load->model('user_model');
                        $this->user_model->insert_item($finfo);
                    }
            
                    # Load the view on here
            
                }
            }
            

            【讨论】:

            • 尝试了你的代码,但没有用.. 提宝贵意见
            • 与您没有选择文件相同的错误。以及如何将文件路径保存在DB、IN CI中
            • 你添加了这个if(!$this-&gt;upload-&gt;do_upload('image'))
            • $finfo 返回 array.so 以获取您需要更改的文件名
            • 是的,我用过 if(!$this->upload->do_upload('image')) 但不知道
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-10-22
            • 1970-01-01
            • 2011-09-22
            • 1970-01-01
            • 1970-01-01
            • 2017-01-16
            相关资源
            最近更新 更多