我想使用CodeIgniter将图像上传到数据库我使用以下代码。现在它显示错误“您没有选择要上传的文件”。即使我选择了。任何人都可以说我的代码有什么错误。
<?php
class Register_form_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library('form_validation','validation');
}
function index()
{
$this->form_validation->set_rules('name','name','required');
$this->form_validation->set_rules('age','age','required');
$this->form_validation->set_rules('gender','gender','required');
$this->form_validation->set_rules('mobilno','mobilno','required');
$this->form_validation->set_rules('email','email','required|valid_email|is_unique[register.email]');
$this->form_validation->set_rules('uname','username','required');
$this->form_validation->set_rules('password','password','required|matches[cpassword]');
$this->form_validation->set_message('cpassword','password mismatched','required');
$this->form_validation->set_rules('cpassword','cpassword','required');
if($this->form_validation->run() == FALSE)
{
$this->load->view("Register_form_view");
}
else
{
$this->doregister();
}
}
function doregister()
{
if($this->input->post("submit"))
{
$config = array( "upload_path" => "image/",
"allowed_types" => "jpg|gif|jpeg|png",
"max_size" => "2048",
"max_width" => "",
"max_height" => ""
);
$this->load->library("upload",$config);
if($this->upload->do_upload('image'))
{
$upload_data = $this->upload->data(); //echo"<pre>"; print_r($upload_data); exit;
}
else
{
echo $error = $this->upload->display_errors(); //echo"<pre>"; print_r($error); exit;
}
$name = $this->input->post('name');
$age = $this->input->post('age');
$gender = $this->input->post('gender');
$mobno = $this->input->post('mobilno');
$email = $this->input->post('email');
$uname = $this->input->post('uname');
$pass = $this->input->post('password');
$cpass = $this->input->post('cpassword');
$image = $upload_data['file_name'];
$data = array( 'name' => $name,
'age' => $age,
'gender' => $gender,
'mobilno' => $mobno,
'email' => $email,
'uname' => $uname,
'password' => $pass,
'image' => $image
);
//echo"<pre>"; print_r($data); exit;
//$this->load->library('form_validation');
$this->load->model("Register_model","register");
if($this->register->insertData($data))
{
$this->load->library("email");
$this->email->from('magesh0312@gmail.com');
$this->email->to($email);
$this->email->subject('Confirmation message');
$message = "you Registred successfully \n";
$message = "Thank you for joining";
$this->email->message($message);
if($this->email->send())
{
echo 'success';
echo"Registred successfully";
}
else
{
echo "mail has send failed";
echo" but you joined the group";
}
//redirect('Register_form_controller/index');
}
}
else
{
echo"registration failed";
}
}
}
?>
答案 0 :(得分:0)
function doregister()
{
if($this->input->post("submit"))
{
$config['upload_path'] = './image/';
$config['allowed_types'] = 'jpg|gif|jpeg|png';
$config['max_size'] = '2048';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data=$this->upload->data();
$name = $this->input->post('name');
$age = $this->input->post('age');
$gender = $this->input->post('gender');
$mobno = $this->input->post('mobilno');
$email = $this->input->post('email');
$uname = $this->input->post('uname');
$pass = md5($this->input->post('npassword'));// important config md5 password
$cpass = md5($this->input->post('cpassword'))// important config md5 password;
$file= array( 'name' => $name,
'age' => $age,
'gender' => $gender,
'mobilno' => $mobno,
'email' => $email,
'uname' => $uname,
'password' => $pass,
'image' => $data['file_name']
);
$this->load->model("Register_model","register");
if($this->register->insertData($file))
{
$this->load->library("email");
$this->email->from('magesh0312@gmail.com');
$this->email->to($email);
$this->email->subject('Confirmation message');
$message = "you Registred successfully \n";
$message = "Thank you for joining";
$this->email->message($message);
if($this->email->send())
{
echo 'success';
echo"Registred successfully";
redirect('your_page');
}
else
{
echo "mail has send failed";
echo" but you joined the group";
}
}
}
} else {
echo"registration failed";
}
}
答案 1 :(得分:0)
嘿我多次得到同样的错误我的所有php代码总是正确但我总是忘记enctype="multipart/form-data"
表单标签检查你是否在表单中添加了
<form action="demo_post_enctype.asp" method="post" enctype="multipart/form-data">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>
如果不在表单标记中添加enctype="multipart/form-data"
,则codeigniter会始终抱怨You did not select a file to upload
有关在codeigniter中上传图片的详情,请查看link