1

I am uploading images and files through a form. But after the upload the name is not changing. On first function call if the name is i.e profilePic then this file name remains same on every call even i am changing the name in $config['file_name'] parameter.

Function calls:

    function getAndSaveEmployeeDetails() {
    $resume = 'resume';
    $resume = $this -> do_upload($resume, $employeeID);

    $profilePic = 'profilePic';
    $profilePic = $this -> do_upload($profilePic, $employeeID);

    $cnic = 'cnicScannedImage';
    $cnicScannedImage = $this -> do_upload($cnic, $employeeID); }

//upload function

public function do_upload($uploadedField, $employeeID) {
//$uploadedField = $this -> input -> post('_hidden_field');
    if (empty($_FILES[$uploadedField]['name'])) {

        $error = "File is empty please choose a file";
        return $error;

    } else {
        $name = '_' . $uploadedField;
        $config['file_name'] = $employeeID . $name;
        $config['upload_path'] = './uploads/' . $employeeID;
        $config['allowed_types'] = 'gif|jpg|jpeg|png|iso|dmg|zip|rar|doc|docx|xls|xlsx|ppt|pptx|csv|ods|odt|odp|pdf|rtf|sxc|sxi|txt|exe|avi|mpeg|mp3|mp4|3gp';
        $config['max_size'] = 2048;
        $config['max_width'] = 0;
        $config['max_height'] = 0;
        $this -> load -> library('upload', $config);

        if (!is_dir('uploads')) {
            mkdir('./uploads', 0777, true);
        }
        $dir_exist = true;
        // flag for checking the directory exist or not
        if (!is_dir('uploads/' . $employeeID)) {
            mkdir('./uploads/' . $employeeID, 0777, true);
            $dir_exist = false;
            // dir not exist
        } else {

        }

        if (!$this -> upload -> do_upload($uploadedField)) {

            if (!$dir_exist)
                rmdir('./uploads/' . $employeeID);

            $error = array('error' => $this -> upload -> display_errors());
            return $error;

            //$this -> load -> view('upload_form', $error);
        } else {
            $data = array('upload_data' => $this -> upload -> data());

            return $path = $data['upload_data']['full_path'];

        }
    }
}

Please look at the image in the below link you will understand the behavior

These are the file names and it is caching the first name passed in function call

1 Answer 1

4

Load upload library in the first line of getAndSaveEmployeeDetails(), using

$this->load->library('upload');

without passing any config.

Then in do_upload($uploadedField, $employeeID) function after defining your config array, don't use $this->load->library('upload', $config); but use $this->upload->initialize($config);

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Its working. Can you please tell me the difference between $this->load->library('upload', $config); vs $this->upload->initialize($config);
The upload library was initialized with a config in the first function call which has resume as $uploadedField. In the other function calls, the upload library won't be loaded because it is already. The trick is to load it without any config then initialize it with a config specific to each function call.

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.