由于 PHP 中的"Call to a member function X on a non-object",我无法上传图像



遇到
PHP 错误 严重性:通知

消息:未定义的属性:Ncatering::$upload

文件名:控制器/Ncatering.php

行号:134

回溯:

文件: C:\xampp\htdocs\National\admin\application\controllers\Ncatering.php 行: 134
功能:_error_handler

文件: C:\xampp\htdocs\National\admin\index.php 行: 315 功能:require_once

致命错误:在第 134 行的 C:\xampp\htdocs\National\admin\application\controllers\Ncatering.php 中对非对象调用成员函数 do_upload( 遇到 PHP 错误 严重性:错误

消息:调用非对象上的成员函数 do_upload((

文件名:控制器/Ncatering.php

行号:134

回溯:

这是我的控制器:

 function add_gallery()
{
    //$this->load->model('Mcatering');
    $config['upload_path'] = './upload/';
    $config['allowed_types'] = 'gif|jpg|png|mp3';
    $this->load->library('upload',$config);
    $this->upload->do_upload('userfile'); 
        $data = array('upload_data' => $this->upload->data());
        $img = $this->upload->data();
        $imgname = $img['file_name'];
        $sss=$this->input->post('sss');
        $data=array('image'=>$imgname,'sss'=>$sss);
        //$data=array('sss'=>$sss);
        $this->Mcatering->insertgallery($data);
               redirect('Ncatering/gallery');

这是我上传图片的表格:

          <?php echo form_open_multipart('Ncatering/add_gallery/'); ?>
               <div class="box-body">
               <div class="form-group">
              <label for="exampleInputFile">Upload Image</label>
              <input type="file" name="userfile" accept="image/jpeg" 
       width="900" height="600" id="exampleInputFile">
            </div>
             </div>
          <div class="box-footer">
            <button type="submit" class="btn" style="background-color:#FC0;">Submit</button>
          </div>
       <?php echo form_close(); ?>

您的代码似乎没有任何问题。 您可以尝试使用核心PHP函数move_uploaded_file()上传文件

                    $target_dir = "upload/";
                    $imgName1 = time().basename($_FILES["userfile"]["name"]);
                    $imgName= preg_replace('/s+/', '', $imgName1);
                    $target_file = $target_dir . $imgName;
                    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
                    move_uploaded_file($_FILES["userfile"]["tmp_name"], $target_file);

您的文件名将存储在变量$imgName

它对我来说很好用...以下功能有助于图片上传...

在视图中的文件

<?php 
    $attributes = array('class' => 'form-horizontal', 'id' => 'event_form');
    echo form_open_multipart('events/create', $attributes); 
?>
<div class="row">
    <div class="col-md-6">
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-4 control-label">Upload Images</label>
            <div class="col-sm-8">
                <input type="file" name="events_file" id="gallery-photo-add">
                <?php echo form_error('events_file', '<div class="error">', '</div>'); ?>
            </div>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-md-offset-3" style="margin-top: 10px;">
        <button type="submit" class="btn btn-primary">Submit</button>
    </div>
</div>
<?php form_close(); ?>

在控制器中

<?php
public function create()
{
    $web = array();
    $web['title'] = 'Events List create';
    $web['content'] = 'web/events';
    $web['isNewRecord'] = 1;
     if (empty($_FILES['events_file']['name'])) {
        $this->form_validation->set_rules('events_file', 'Images', 'required');             
     }
    if ($this->form_validation->run() == FALSE) {
        $this->load->view('web_template',$web);
    } else {
        $files = $_FILES;
        $count = count($_FILES['events_file']['name']);
        $_FILES['events_file']['name']= time().'_'.$files['events_file']['name'];
        $_FILES['events_file']['type']= $files['events_file']['type'];
        $_FILES['events_file']['tmp_name']= $files['events_file']['tmp_name'];
        $_FILES['events_file']['error']= $files['events_file']['error'];
        $_FILES['events_file']['size']= $files['events_file']['size'];
        $uploadPath = 'uploads/events/';
        $config['upload_path'] = $uploadPath;
        $config['allowed_types'] = 'gif|jpg|png';
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        if($this->upload->do_upload('events_file')){
            $fileData = $this->upload->data();
            $uploadData['event_id'] = $insert_id;
            $uploadData['image'] = $fileData['file_name'];
        }
        if(!empty($uploadData)){
            $this->db->insert('events_image',$uploadData);   
        }
    }
}

相关内容

最新更新