繁体   English   中英

Codeigniter中的文件上传错误

[英]File uploading error in Codeigniter

我正在尝试在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只是为了检查是否发生任何错误。 它显示“您没有选择要上传的文件。” 即使选择文件,也请帮我。

请参考此代码。 这肯定会为您工作

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

        }
    }

更改此:

<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

    }
}

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

<form method="post" enctype="multipart/form-data">
    <input type="file" name="field_name"/>
</form>

<?php
 $this->upload->do_upload('field_name');
 ?>

尝试这个:

查看页面

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

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

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

在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);
}
}
}
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM