遇到一个PHP错误
严重性:注意
消息:未定义的属性:N餐饮::$上传
文件名:控制器/Ncatering.php
行号:134
回溯:
File: C:\xampp\htdocs\National\admin\应用程序\控制器\Ncatering.php行:134
功能:_error_handler
文件:C:\xampp\htdocs\National\admin\index。php行:315函数:需要一次
致命错误:对C:\xampp\htdocs\National\admin\application\controllers\n目录中的非对象调用成员函数do_upload()。php第134行遇到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\u uploaded\u 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);
}
}
}