【问题标题】:Codeigniter 3.0: File name is not changing after uploadingCodeigniter 3.0:上传后文件名没有改变
【发布时间】:2017-10-01 07:54:03
【问题描述】:

我正在通过表单上传图像和文件。但上传后名称并没有改变。在第一次函数调用时,如果名称是 profilePic,那么即使我在 $config['file_name'] 参数中更改名称,这个文件名在每次调用时都保持不变。

函数调用:

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

//上传函数

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'];

        }
    }
}

请查看以下链接中的图片,您将了解行为

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

【问题讨论】:

    标签: php codeigniter-3


    【解决方案1】:

    getAndSaveEmployeeDetails()的第一行加载upload库,使用

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

    不通过任何配置。

    然后在do_upload($uploadedField, $employeeID)函数定义你的配置数组后,不要使用$this->load->library('upload', $config); 但使用$this->upload->initialize($config);

    【讨论】:

    • 感谢它的工作。你能告诉我 $this->load->library('upload', $config); 之间的区别吗? vs $this->upload->initialize($config);
    • 上传库在第一个函数调用中使用配置初始化,恢复为$uploadedField。在其他函数调用中,不会加载上传库,因为它已经加载了。诀窍是在没有任何配置的情况下加载它,然后使用特定于每个函数调用的配置对其进行初始化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 1970-01-01
    • 2014-04-24
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    相关资源
    最近更新 更多