[英]Image upload using codeigniter error
我正在嘗試在代碼點火器中制作圖像上傳表單,但出現以下錯誤。
嚴重程度:注意
消息:未定義的索引:image_file
文件名:controllers / men.php
行號:23
回溯:
文件:C:\\ xampp \\ htdocs \\ CodeIgniter-3.1.8 \\ application \\ controllers \\ men.php行:23函數:_error_handler
文件:C:\\ xampp \\ htdocs \\ CodeIgniter-3.1.8 \\ index.php行:315功能:require_once
1)查看
<!DOCTYPE html>
<html>
<head>
<title>image</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>/public/css/bootstrap.css">
</head>
<body>
<form method="post" action="<?php echo base_url()?>men/image_upload" id="upload_form" />
<input type="file" name="image_file">
<input type="submit" id="upload" name="upload" value="Upload" />
</form>
</body>
</html>
2)控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Men extends CI_Controller
{
public function index(){
$this->load->view("imageupload");
}
public function image_upload(){
$config['upload_path']='./uploads';
$config['allowed_types']='*';
$this->load->library('upload',$config);
$this->upload->do_upload('image_file');
$image_file = $this->upload->data();
$data=array('image_file'=> $image_file['image_file']);
$this->load->model("mymodel");
$this->mymodel->imagedone($data);
}
}
?>
3)型號
<?php
class Mymodel extends CI_Model{
function imagedone($data){
$query = $this->db->insert("image_tbl",$data);
if($query)
{
echo "File is uploaded";
}
else{
echo "failure";
}
}
}
此錯誤可能是由於缺少表單標記的屬性而引起的,請在表單標記中添加-> enctype =“ multipart / form-data”屬性,然后嘗試執行以下操作:
<form method="post" action="<?php echo base_url()?>men/image_upload" id="upload_form" enctype="multipart/form-data" /> <input type="file" name="image_file"> <input type="submit" id="upload" name="upload" value="Upload" /> </form>
首先,如果要上傳文件,則需要在表單中添加enctype屬性。 將enctype = multipart / form-data屬性添加到表單,然后重試。
<!DOCTYPE html>
<html>
<head>
<title>image</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?
>/public/css/bootstrap.css">
</head>
<body>
<form method="post" enctype="multipart/form-data" action="<?php echo base_url()?>men/image_upload"
id="upload_form" />
<input type="file" name="image_file">
<input type="submit" id="upload" name="upload" value="Upload" />
</form>
</body>
</html>
在表單中使用此屬性enctype =“ multipart / form-data”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.