php - Codeigniter : Check if file_exist in Cpanel

353

enter image description here

I want check if my images is exist in folder public_html/monitoring/images/user/$fileimage , after that i want deleted it and upload new one.

I have model to check if my image exist in that folder with this code :

My Images is 74139316_9273.jpg

Model

public function check_image_exist($username)
    {
        $this->db->select('fotoUser');
        $this->db->from('app_user');
        $this->db->where('username', $username);
        $selectOldImage = $this->db->get()->row()->fotoUser;
        if (file_exists('../monitoring/images/user/images/' . $selectOldImage)) {
            unlink('./images/' . $selectOldImage);
            return true;
        } else {
            return false;
        }
    }

And this my controller to upload new image and update it.

Controller

 public function updateImageProfilInfo_post()
    {
        $username = $this->input->post('username');

        $config['upload_path'] = '../monitoring/images/user/images/';
        $config['allowed_types'] = 'jpg|png|jpeg';
        $config['max_size'] = '2000'; // max size in KB
        $config['overwrite'] = TRUE; // For Replace Image name

        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        $check_image_exist = $this->user_model->check_image_exist($username);
        if ($check_image_exist) {
            // print_r($check_image_exist);
            if (!$this->upload->do_upload('file')) {
                $errorArray = strip_tags($this->upload->display_errors());
                $this->response(
                    ["status" => "error", "message" => "Failed To Upload Image $errorArray"],
                    REST_Controller::HTTP_BAD_REQUEST
                );
            } else {
                $info = $this->upload->data();
                $image_path = $info['raw_name'] . $info['file_ext'];
                $updateImageProfilInfo = $this->user_model->update_image_profil_info_user($username, $image_path);
                if ($updateImageProfilInfo) {
                    $this->response(
                        ["status" => "ok", "message" => "Success Update Image $username"],
                        REST_Controller::HTTP_OK
                    );
                } else {
                    $this->response(
                        ["status" => "error", "message" => "Failed To Update $username Image Profil , Try Again Later ..."],
                        REST_Controller::HTTP_BAD_REQUEST
                    );
                }
            }
        } else {
            $this->response(
                ["status" => "error", "message" => "Failed To Find Image with Username $username. Try Again Later ..."],
                REST_Controller::HTTP_NOT_FOUND
            );
        }

enter image description here

But the problem is my image is always not found, although I'm sure the directory is correct. How can i fixed it ?

Thanks

People are also looking for solutions to the problem: arrays - How to get directory name from file path using php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.