Codeigniter-使用用户添加表格上传照片

时间:2018-06-23 00:11:29

标签: php codeigniter upload photo photo-upload

我想知道...

我永远无法将任何照片上传到我的文件夹和数据库中。我试了上千遍。我做错了什么,请帮帮我。

我可以添加用户。没问题。但是我尝试了照片上传,但从未上传任何照片。我的代码:

控制器:

     public function insert () {

            $config['upload_path'] = './upload/user/';
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = 1024;
            $config['max_width'] = 100;
            $config['max_height'] = 100;
            $config['file_name'] = rand(100,999);
            $config['overwrite'] = false;

            $this->load->library('upload', $config);
            if(!$this->upload->do_upload('userImg')) {

                $this->load->view('user');
            }


        $data = array (
        "userUsername" => $this->input->post("userUsername"),
        "userPass" => $this->input->post("userPass"),
        "userEmail" => $this->input->post("userEmail"),
        "userName" => $this->input->post("userName"),
        "userSurname" => $this->input->post("userSurname"),
        "userImg" => $this->input->post("userImg"),
        "userLang" => $this->input->post("userLang"),
        "userType" => $this->input->post("userType"),
        "userDate" => date('Y-m-d H:i:s'),
   );
    $insert = $this->db->insert("user", $data);
    if($insert) {
    redirect(base_url("user"));
   }else {

    redirect(base_url("user"));

   }

}

和我的查看页面添加用户

<form class="cmxform form-horizontal tasi-form" id="signupForm" method="post" enctype="multipart/form-data" action="<?php echo base_url(); ?>user/insert">
        <div class="form-group last">
            <label class="control-label col-md-3">Avatar Upload:</label>
            <div class="col-md-9">
                <div class="fileupload fileupload-new" data-provides="fileupload">
                    <div class="fileupload-new thumbnail" style="width: 200px; height: 150px;">
                        <img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&amp;text=no+image" alt="" />
                    </div>
                    <div class="fileupload-preview fileupload-exists thumbnail" style="max-width: 200px; max-height: 150px; line-height: 20px;"></div>
                    <div>
                           <span class="btn btn-white btn-file">
                           <span class="fileupload-new"><i class="fa fa-paper-clip"></i> Select image</span>
                           <span class="fileupload-exists"><i class="fa fa-undo"></i> Change</span>
                           <input type="file" class="default" name="userImg" />
                           </span>
                    </div>
                </div>
                <span class="label label-danger">NOTE!</span>
                <span>
                     Please Upload Your Profile Photo!
                </span>
            </div>
        </div>
        <div class="form-group ">
            <label for="username" class="control-label col-lg-2">Username <span class="text-danger">*</span></label>
            <div class="col-lg-10">
                <input class=" form-control" id="username" name="userUsername" type="text" required="required" />
            </div>
        </div>
        <div class="form-group ">
            <label for="password" class="control-label col-lg-2">Password <span class="text-danger">*</span></label>
            <div class="col-lg-10">
                <input class="form-control " id="password" name="userPass" type="password" required="required" />
            </div>
        </div>
         <div class="form-group ">
            <label for="email" class="control-label col-lg-2">E-Mail <span class="text-danger">*</span></label>
            <div class="col-lg-10">
                <input class="form-control " id="email" name="userEmail" type="email" required="required" />
            </div>
        </div>

.....

为此帮助我。谢谢大家。


编辑用户控制器:

public function update($userId) {
        if (isset($_FILES)) {
            $config['upload_path'] = './upload/user';
            $config['allowed_types']        = 'jpeg|jpg|png|gif';
            $config['max_size'] = 5000;
            $config['max_width'] = 4024;
            $config['max_height'] = 4068;
            $new_name = $this->input->post("userUsername");
            $config['file_name'] = $new_name;
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            if ( ! $this->upload->do_upload('userImg'))
            {
                $error = array('error' => $this->upload->display_errors());
                echo json_encode($error);
            }
            else{
                $upload_data = $this->upload->data();
                $data = array (
                    "userUsername" => $this->input->post("userUsername"),
                    "userPass" => $this->input->post("userPass"),
                    "userEmail" => $this->input->post("userEmail"),
                    "userName" => $this->input->post("userName"),
                    "userSurname" => strtoupper($this->input->post("userSurname")),
                    "userImg" => $upload_data['file_name'],
                    "userLang" => $this->input->post("userLang"),
                    "userType" => $this->input->post("userType"),
                    "userDate" => date('Y-m-d H:i:s'),
                );
                $update = $this->db->where("userId", $userId)->update("user", $data);
                if($update) {
                    redirect(base_url("user"));
                }else {
                    echo "Hata!";
                }
            }
        }
        else{
            $data = array (
                "userUsername" => $this->input->post("userUsername"),
                "userPass" => $this->input->post("userPass"),
                "userEmail" => $this->input->post("userEmail"),
                "userName" => $this->input->post("userName"),
                "userSurname" => strtoupper($this->input->post("userSurname")),
                "userLang" => $this->input->post("userLang"),
                "userType" => $this->input->post("userType"),
                "userDate" => date('Y-m-d H:i:s'),
            );
            $update = $this->db->where("userId", $userId)->update("user", $data);
            if($update) {
                redirect(base_url("user"));
            }else {
                echo "Hata!";
            }
        }
    }

编辑用户视图:

<div class="form">
                            <form class="cmxform form-horizontal tasi-form" id="signupForm" method="post" action="<?php echo base_url("user/update/$user->userId"); ?>">
                                <div class="form-group last">
                                    <label class="control-label col-md-3">Avatar Upload:</label>
                                    <div class="col-md-9">
                                        <div class="fileupload fileupload-new" data-provides="fileupload" value="<?php echo $user->userImg; ?>">
                                            <div class="fileupload-new thumbnail" style="width: 200px; height: 150px;">
                                                <img src="<?php echo base_url().'upload/user/'.$user->userImg; ?> alt="" />
                                            </div>
                                            <div class="fileupload-preview fileupload-exists thumbnail" style="max-width: 200px; max-height: 150px; line-height: 20px;"></div>
                                            <div>
                                                   <span class="btn btn-white btn-file">
                                                   <span class="fileupload-new"><i class="fa fa-paper-clip"></i> Select image</span>
                                                   <span class="fileupload-exists"><i class="fa fa-undo"></i> Change</span>
                                                   <input type="file" class="default" name="userImg" />
                                                   </span>
                                            </div>
                                        </div>
                                        <span class="label label-danger">NOTE!</span>
                                        <span>
                                             Please Upload Your Profile Photo!
                                        </span>
                                    </div>
                                </div>
                                <div class="form-group ">
                                    <label for="username" class="control-label col-lg-2">Username <span class="text-danger">*</span></label>
                                    <div class="col-lg-10">
                                        <input class=" form-control" id="username" name="userUsername" type="text" value="<?php echo $user->userUsername; ?>" />
                                    </div>
                                </div>
                                <div class="form-group ">
                                    <label for="password" class="control-label col-lg-2">Password <span class="text-danger">*</span></label>
                                    <div class="col-lg-10">
                                        <input class="form-control " id="password" name="userPass" type="password" value="<?php echo $user->userPass; ?>" readonly />
                                    </div>
                                </div>
                                 <div class="form-group ">
                                    <label for="email" class="control-label col-lg-2">E-Mail <span class="text-danger">*</span></label>
                                    <div class="col-lg-10">
                                        <input class="form-control " id="email" name="userEmail" type="email" value="<?php echo $user->userEmail; ?>" />
                                    </div>
                                </div>
                                <div class="form-group ">
                                    <label for="firstname" class="control-label col-lg-2">First Name <span class="text-danger">*</span></label>
                                    <div class="col-lg-10">
                                        <input class=" form-control" id="firstname" name="userName" type="text" value="<?php echo $user->userName; ?>" />
                                    </div>
                                </div>
                                <div class="form-group ">
                                    <label for="lastname" class="control-label col-lg-2">Last Name <span class="text-danger">*</span></label>
                                    <div class="col-lg-10">
                                        <input class="form-control " id="lastname" name="userSurname" type="text" value="<?php echo $user->userSurname; ?>" />
                                    </div>
                                </div>

                                <!-- Basic select -->
                                    <div class="form-group">
                                        <label class="control-label col-lg-3">Choose Language <span class="text-danger">*</span></label>
                                        <div class="col-lg-9">
                                            <select name="userLang" class="form-control">
                                                <option value="<?php echo $user->userLang; ?>"><?php echo $user->userLang; ?></option>
                                                    <option value="en">English</option>
                                                    <option value="ar">Arabic</option>
                                            </select>
                                        </div>
                                    </div>
                                    <!-- /basic select -->

                                    <!-- Basic select -->
                                    <div class="form-group">
                                        <label class="control-label col-lg-3">User Type <span class="text-danger">*</span></label>
                                        <div class="col-lg-9">
                                            <select name="userType" class="form-control">
                                                <option value="<?php echo $user->userType; ?>"><?php echo $user->userType; ?></option>
                                                <option value="1">Admin = 1</option>
                                                <option value="2">Yönetici = 2</option>
                                                <option value="3">Departman Sorumlusu = 3</option>
                                                <option value="4">Operatör = 4</option>
                                                <option value="5">Stajyer = 5</option>
                                            </select>
                                        </div>
                                    </div>
                                    <!-- /basic select -->

                                <!-- Basic select -->
                                <div class="form-group">
                                    <label class="control-label col-lg-3">Status <span class="text-danger">*</span></label>
                                    <div class="col-lg-9">
                                        <select name="userStatus" class="form-control">
                                            <option value="<?php echo $user->userStatus; ?>"><?php echo $user->userStatus; ?></option>
                                            <option value="1">Active</option>
                                            <option value="0">Deactive</option>
                                        </select>
                                    </div>
                                </div>
                                <!-- /basic select -->
                                <div class="form-group">
                                    <div class="col-lg-offset-2 col-lg-10">
                                        <button class="btn btn-success" type="submit" value="Upload">Edit User</button>
                                        <button class="btn btn-default" type="reset">Reset</button>
                                        <button class="btn btn-danger" type="cancel">Cancel</button>
                                    </div>
                                </div>
                            </form>
                        </div>

3 个答案:

答案 0 :(得分:1)

使用此代码进行更新。在视图和控制器中。

 <div class="form">
                            <form class="cmxform form-horizontal tasi-form" id="signupForm" enctype="multipart/form-data" method="post" action="<?php echo base_url("user/update/$user->userId"); ?>">
                                <div class="form-group last">
                                    <label class="control-label col-md-3">Avatar Upload:</label>
                                    <div class="col-md-9">
                                        <div class="fileupload fileupload-new" data-provides="fileupload">
                                            <div class="fileupload-new thumbnail" style="width: 200px; height: 150px;">
                                                <img src="<?php echo base_url().'upload/user/'.$user->userImg; ?> alt="" />
                                            </div>
                                            <div class="fileupload-preview fileupload-exists thumbnail" style="max-width: 200px; max-height: 150px; line-height: 20px;"></div>
                                            <div>
                                                   <span class="btn btn-white btn-file">
                                                   <span class="fileupload-new"><i class="fa fa-paper-clip"></i> Select image</span>
                                                   <span class="fileupload-exists"><i class="fa fa-undo"></i> Change</span>
                                                   <input type="file" class="default" name="userImg" />
                                                   </span>
                                            </div>
                                        </div>
                                        <span class="label label-danger">NOTE!</span>
                                        <span>
                                             Please Upload Your Profile Photo!
                                        </span>
                                    </div>
                                </div>
                                <div class="form-group ">
                                    <label for="username" class="control-label col-lg-2">Username <span class="text-danger">*</span></label>
                                    <div class="col-lg-10">
                                        <input class=" form-control" id="username" name="userUsername" type="text" value="<?php echo $user->userUsername; ?>" />
                                    </div>
                                </div>
                                <div class="form-group ">
                                    <label for="password" class="control-label col-lg-2">Password <span class="text-danger">*</span></label>
                                    <div class="col-lg-10">
                                        <input class="form-control " id="password" name="userPass" type="password" value="<?php echo $user->userPass; ?>" readonly />
                                    </div>
                                </div>
                                 <div class="form-group ">
                                    <label for="email" class="control-label col-lg-2">E-Mail <span class="text-danger">*</span></label>
                                    <div class="col-lg-10">
                                        <input class="form-control " id="email" name="userEmail" type="email" value="<?php echo $user->userEmail; ?>" />
                                    </div>
                                </div>
                                <div class="form-group ">
                                    <label for="firstname" class="control-label col-lg-2">First Name <span class="text-danger">*</span></label>
                                    <div class="col-lg-10">
                                        <input class=" form-control" id="firstname" name="userName" type="text" value="<?php echo $user->userName; ?>" />
                                    </div>
                                </div>
                                <div class="form-group ">
                                    <label for="lastname" class="control-label col-lg-2">Last Name <span class="text-danger">*</span></label>
                                    <div class="col-lg-10">
                                        <input class="form-control " id="lastname" name="userSurname" type="text" value="<?php echo $user->userSurname; ?>" />
                                    </div>
                                </div>

                                <!-- Basic select -->
                                    <div class="form-group">
                                        <label class="control-label col-lg-3">Choose Language <span class="text-danger">*</span></label>
                                        <div class="col-lg-9">
                                            <select name="userLang" class="form-control">
                                                <option value="<?php echo $user->userLang; ?>"><?php echo $user->userLang; ?></option>
                                                    <option value="en">English</option>
                                                    <option value="ar">Arabic</option>
                                            </select>
                                        </div>
                                    </div>
                                    <!-- /basic select -->

                                    <!-- Basic select -->
                                    <div class="form-group">
                                        <label class="control-label col-lg-3">User Type <span class="text-danger">*</span></label>
                                        <div class="col-lg-9">
                                            <select name="userType" class="form-control">
                                                <option value="<?php echo $user->userType; ?>"><?php echo $user->userType; ?></option>
                                                <option value="1">Admin = 1</option>
                                                <option value="2">Yönetici = 2</option>
                                                <option value="3">Departman Sorumlusu = 3</option>
                                                <option value="4">Operatör = 4</option>
                                                <option value="5">Stajyer = 5</option>
                                            </select>
                                        </div>
                                    </div>
                                    <!-- /basic select -->

                                <!-- Basic select -->
                                <div class="form-group">
                                    <label class="control-label col-lg-3">Status <span class="text-danger">*</span></label>
                                    <div class="col-lg-9">
                                        <select name="userStatus" class="form-control">
                                            <option value="<?php echo $user->userStatus; ?>"><?php echo $user->userStatus; ?></option>
                                            <option value="1">Active</option>
                                            <option value="0">Deactive</option>
                                        </select>
                                    </div>
                                </div>
                                <!-- /basic select -->
                                <div class="form-group">
                                    <div class="col-lg-offset-2 col-lg-10">
                                        <button class="btn btn-success" type="submit" value="Upload">Edit User</button>
                                        <button class="btn btn-default" type="reset">Reset</button>
                                        <button class="btn btn-danger" type="cancel">Cancel</button>
                                    </div>
                                </div>
                            </form>
                        </div>

像这样的控制器。

public function update($userId) {
        if (isset($_FILES) && $_FILES['userImg']['error'] == '0') {
            $config['upload_path'] = './upload/user';
            $config['allowed_types']        = 'jpeg|jpg|png|gif';
            $config['max_size']             = 1024;
            $config['max_width']            = 1000;
            $config['max_height']           = 1000;
            $new_name = $this->input->post("userUsername");
            $config['file_name'] = $new_name;
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            if ( ! $this->upload->do_upload('userImg'))
            {
                $error = array('error' => $this->upload->display_errors());
                echo json_encode($error);
            }
            else{
                $upload_data = $this->upload->data();
                $data = array (
                    "userUsername" => $this->input->post("userUsername"),
                    "userPass" => $this->input->post("userPass"),
                    "userEmail" => $this->input->post("userEmail"),
                    "userName" => $this->input->post("userName"),
                    "userSurname" => strtoupper($this->input->post("userSurname")),
                    "userImg" => $upload_data['file_name'],
                    "userLang" => $this->input->post("userLang"),
                    "userType" => $this->input->post("userType"),
                    "userDate" => date('Y-m-d H:i:s'),
                );
                $update = $this->db->where("userId", $userId)->update("user", $data);
                if($update) {
                    redirect(base_url("user"));
                }else {
                    echo "Hata!";
                }
            }
        }
        else{
            $data = array (
                "userUsername" => $this->input->post("userUsername"),
                "userPass" => $this->input->post("userPass"),
                "userEmail" => $this->input->post("userEmail"),
                "userName" => $this->input->post("userName"),
                "userSurname" => strtoupper($this->input->post("userSurname")),
                "userLang" => $this->input->post("userLang"),
                "userType" => $this->input->post("userType"),
                "userDate" => date('Y-m-d H:i:s'),
            );
            $update = $this->db->where("userId", $userId)->update("user", $data);
            if($update) {
                redirect(base_url("user"));
            }else {
                echo "Hata!";
            }
        }
    }

答案 1 :(得分:0)

如果您的网址正确并且图片没有错误,请使用此选项,它可以正常工作并上传图片。我测试过了。

public function insert () {
    $config['upload_path'] = './upload/user';
    $config['allowed_types']        = 'jpeg|jpg|png|gif';
    $config['max_size']             = 1024;
    $config['max_width']            = 1000;
    $config['max_height']           = 1000;
    $new_name = time();
    $config['file_name'] = $new_name;
    $this->load->library('upload', $config);
    $this->upload->initialize($config);
    if ( ! $this->upload->do_upload('userImg'))
    {
        $error = array('error' => $this->upload->display_errors());
            echo json_encode($error);
    }
    else{
    $upload_data = $this->upload->data();
    $data = array (
        "userUsername" => $this->input->post("userUsername"),
        "userPass" => $this->input->post("userPass"),
        "userEmail" => $this->input->post("userEmail"),
        "userName" => $this->input->post("userName"),
        "userSurname" => $this->input->post("userSurname"),
        "userImg" => $upload_data['file_name'],
        "userLang" => $this->input->post("userLang"),
        "userType" => $this->input->post("userType"),
        "userDate" => date('Y-m-d H:i:s'),
    );
    $insert = $this->db->insert("user", $data);
    if($insert) {
        redirect(base_url("user"));
    }else {

        redirect(base_url("user"));

    } 
  }
}

答案 2 :(得分:0)

要进行更新,您可以使用此代码

public function update($userId) {
        if (isset($_FILES) && $_FILES['userImg']['error'] == '0') {
            $config['upload_path'] = './upload/user';
            $config['allowed_types']        = 'jpeg|jpg|png|gif';
            $config['max_size']             = 1024;
            $config['max_width']            = 1000;
            $config['max_height']           = 1000;
            $new_name = $this->input->post("userUsername");
            $config['file_name'] = $new_name;
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            if ( ! $this->upload->do_upload('userImg'))
            {
                $error = array('error' => $this->upload->display_errors());
                echo json_encode($error);
            }
            else{
                $upload_data = $this->upload->data();
                $data = array (
                    "userUsername" => $this->input->post("userUsername"),
                    "userPass" => $this->input->post("userPass"),
                    "userEmail" => $this->input->post("userEmail"),
                    "userName" => $this->input->post("userName"),
                    "userSurname" => strtoupper($this->input->post("userSurname")),
                    "userImg" => $upload_data['file_name'],
                    "userLang" => $this->input->post("userLang"),
                    "userType" => $this->input->post("userType"),
                    "userDate" => date('Y-m-d H:i:s'),
                );
                $update = $this->db->where("userId", $userId)->update("user", $data);
                if($update) {
                    redirect(base_url("user"));
                }else {
                    echo "Hata!";
                }
            }
        }
        else{
            $data = array (
                "userUsername" => $this->input->post("userUsername"),
                "userPass" => $this->input->post("userPass"),
                "userEmail" => $this->input->post("userEmail"),
                "userName" => $this->input->post("userName"),
                "userSurname" => strtoupper($this->input->post("userSurname")),
                "userLang" => $this->input->post("userLang"),
                "userType" => $this->input->post("userType"),
                "userDate" => date('Y-m-d H:i:s'),
            );
            $update = $this->db->where("userId", $userId)->update("user", $data);
            if($update) {
                redirect(base_url("user"));
            }else {
                echo "Hata!";
            }
        }
    }