我正在尝试使用以下代码上传图片。但是当我上传图片时,我收到了一条错误信息undefined index:image 错误消息:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: image
Filename: controllers/home.php
Line Number: 86
Backtrace:
File: C:\softwares\xamp\htdocs\ASOFT\Projects\CG_Pack\application\controllers\home.php
Line: 86
Function: _error_handler
File: C:\softwares\xamp\htdocs\ASOFT\Projects\CG_Pack\index.php
Line: 293
Function: include_once
控制器
public function view()
{
$data=$this->data;
$data['error']= '' ;
$this->load->view('display',$data);
$path = $_FILES['image']['name'];
$imgext=strtolower(strrchr($path,'.'));
$imgname= $this->generateRandomString().$imgext;
if($path!='')
{
$im= $this->config->item('base_url').'/uploads'.'/'.$imgname;
$x=$this->do_upload($imgname);
$data['img']=$im;
if ($x['error']=='')
{
$this->user_model->getimage($data['img']);
}
else
{
$data['error']= $x['error'] ;
$this->load->view('display',$data);
}
}
}
function generateRandomString()
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$randomString = '';
for ($i = 0; $i < 8; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
function do_upload($img)
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024 ';
$config['file_name'] = $img;
$this->load->library('upload',$config);
if ( ! $this->upload->do_upload('image'))
{
$error = array('error' => $this->upload->display_errors());
$data['error']=$error;
return $data;
}
else
{
$data = array('upload_data' => $this->upload->data());
$data['error']='';
return $data;
}
return;
}
型号:
public function getimage($data)
{
$var=array('img'=>$data);
$this->db->insert('login',$var);
$query = $this->db->get('login');
return $query;
}
查看:
<?php
if($error!='') { ?>
<script>
//print_r($error['error']);
//alert('<?php echo $error['error']?>');
alert('Please upload less than 1 mb file');
</script>
<?php }?>
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#mymodal">Add Image</button>
<!-- Modal -->
<div class="modal fade" id="mymodal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h2>Upload image</h2>
<form action="<?=site_url('home/view')?>" method="post" enctype="multipart/form-data"/>
<input type="file" class="upload" name="image" id="file" required value="<?php echo set_value('image'); ?>" />
<!-- <input type="submit" name="btn-reg" value="Upload" /> -->
<script type="text/javascript">
$(document).on('change','.upload',function(){
files = this.files;
size = files[0].size;
//max size 50kb => 50*1000
if( size > 1000141){
alert('Please upload less than 1mb file');
return false;
}
return true;
});
</script>
<div class="btn-group">
<button class="btn">Choose your div</button>
<button class="btn dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu">
<li><a tabindex="-1" href="#">div 1</a></li>
<li class="disabled"><a tabindex="-1" href="#">div 2</a></li>
<li><a tabindex="-1" href="#">div 3</a></li>
</ul>
</div>
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</form>
</div>
</div>
</div>
</div>
这里我列出了控制器,视图和模型。当我上传图像时,我收到错误消息undefined index。在视图输入字段也是图像,但我收到了错误消息。请为此提供解决方案。