【问题标题】:I can't upload image using codeigniter我无法使用 codeigniter 上传图片
【发布时间】:2015-07-13 10:16:16
【问题描述】:

我想使用 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";
        }
    }
}
?> 

【问题讨论】:

  • 图像路径放在应用程序文件夹之外??
  • 你遇到了什么问题?
  • 我无法上传图片它显示错误消息“您没有选择要上传的文件。”即使我选择了图片
  • @wolfgang1983 但这不是问题,我改了好几次。

标签: php image codeigniter file-upload


【解决方案1】:
     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";
           }
        }

【讨论】:

  • 感谢您的回答,但我收到同样的错误“您没有选择要上传的文件..”
  • 我使用 CI 编写了简单的注册表单。我想在上面添加图片,比如选择个人资料图片。
  • 对不起,错误出现在我的表单上,不在我提到的代码中,再次抱歉
【解决方案2】:

嘿,我多次遇到同样的错误,我的所有 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

【讨论】:

  • 是的,我用过,但很抱歉我使用 form_open 打开了另一个表单,只有我得到了那个错误。谢谢你我清除了那个错误。
猜你喜欢
  • 2012-12-31
  • 2020-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多