-1

I want to know...

I never can upload any photo to my folder and database. I tried it for thousand times. What I do mistake please help me about it.

I can add user good. It is not problem. But I tried it with photo upload which never upload any photo. My codes:

Controller:

     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"));

   }

}

and my view page ADD 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>

.....

Help me for this. Thank you all.


EDIT USER CONTROLLER:

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!";
            }
        }
    }

EDIT USER VIEW:

<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
  • Please provide your error information. Commented Jun 23, 2018 at 1:54
  • @habib - I don't take any error when I add any user. I just can not add photo to my directory and database. Commented Jun 23, 2018 at 9:05
  • @AbdulAhmad Matin - Your solutions are not affected to project. Thanks for your minds. But Can you say other solutions to my problem? I need your help. Commented Jun 23, 2018 at 9:09

3 Answers 3

1

use this code for update. in view and controller.

 <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>

Controller like this.

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!";
            }
        }
    }
Sign up to request clarification or add additional context in comments.

7 Comments

No I take this error: {"error":" The filetype you are attempting to upload is not allowed.<\/p>"} I choose types of png-jpg-jpeg. It gives me an error.
Ok so you only must upload image with this extension jpeg|jpg|png|gif
don't rename your file extension to other extension it not work.
I only upload right extension such as jpeg, jpg, png. But it gives me the same error.
: ( .. remove $config['allowed_types'] = 'jpeg|jpg|png|gif'; but it not a good idea
|
0

use this if your url is correct and image hasn't error it work correctly and image upload . i tested it.

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"));

    } 
  }
}

Comments

0

for update you can use this code

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!";
            }
        }
    }

11 Comments

I don't take any error okay Thank you for this but now no photo upload system when I choose a photo to upload.
if you choose a photo the if loop works and image upload but if you don't choose a file or your file has error else work.
I see. But I choose a photo and upload it with edit user button. It doesn't upload my photo. I cannot see it user directory or database. What can I do about this? My edit button is working correctly because it editing other all information without photo upload. Now I just cannot upload any photo about edit user.
because your image size not fit to current change it to. $config['max_size'] = 5000; $config['max_width'] = 4024; $config['max_height'] = 4068;
No bro, It doesn't work. I changed it to what you said.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.