为什么在此 Codeigniter 3 应用程序中上传之前重命名图像文件失败?

Why does renaming an image file before upload fail in this Codeigniter 3 application?

我正在使用 Codeigniter 3 开发 社交网络 应用程序,Ion-Auth and Bootstrap 4. You can see the Github repo HERE.

更新

我现在有:

if ($this->form_validation->run() === TRUE)
    {
        //more code here

        $config['upload_path'] = './assets/img/avatars';
        $config['file_ext_tolower']     = TRUE;
        $config['allowed_types']        = 'gif|jpg|jpeg|png';
        $config['max_size']             = 1024;
        $config['max_width']            = 1024;
        $config['max_height']           = 1024;
        $config['encrypt_name']         = TRUE;
        $this->load->library('upload', $config);

        if (!$this->upload->do_upload('userfile')){
            $error = array('error' => $this->upload->display_errors());
            $file_name = null;
        } else {
            $file_name = $this->upload->data('file_name');
        }

        $additional_data = [
            'first_name' => $this->input->post('first_name'),
            'last_name' => $this->input->post('last_name'),
            'avatar' =>  $file_name,
            'company' => $this->input->post('company'),
            'phone' => $this->input->post('phone'),
        ];
}

上面的代码在将文件名插入 avatar 列之前正确地对文件名进行了哈希处理,但上传本身从未发生过。

原代码

我正在使用以下代码上传用户图片(头像):

if ($this->form_validation->run() === TRUE)
    {
        //more code here

        $config['upload_path'] = './assets/img/avatars';
        $config['file_ext_tolower']     = TRUE;
        $config['allowed_types']        = 'gif|jpg|jpeg|png';
        $config['max_size']             = 1024;
        $config['max_width']            = 1024;
        $config['max_height']           = 1024;
        $this->load->library('upload', $config);

        if (!$this->upload->do_upload('userfile')){
            $error = array('error' => $this->upload->display_errors());
            $file_name = null;
        } else {
            // get filename with extension
            $file_name = $_FILES['userfile']['name'];
            // get filename without extension
            $file_name_clean = explode('.', $file_name)[0];
            // get filename extension   
            $file_ext = explode('.', $file_name)[1];
            //Add timestamp to filename and hash it
            $file_name = md5($file_name.date('m-d-Y_H:i:s'));
            // add extension
            $file_name = $file_name_clean . '.' . $file_ext;
        }

        $additional_data = [
            'first_name' => $this->input->post('first_name'),
            'last_name' => $this->input->post('last_name'),
            'avatar' =>  $file_name,
            'company' => $this->input->post('company'),
            'phone' => $this->input->post('phone'),
        ];
}

正如您在 else 块中看到的,我通过添加当前时间戳和散列来更改原始文件名。

问题是图像文件本身在上传前没有相应地重命名。以原名上传(图片未存入/assets/img/avatars/

为什么会这样?

如果使用上传

$config['encrypt_name']         = TRUE;

不要使用 $_FILES

改变

$file_name = $_FILES['userfile']['name'];

$file_name = $this->upload->data('file_name');

config.php直接添加你的路径

$config['base_url'] = "http://localhost/yourname/";

试试这个,

$config['upload_path'] = './assets/img/avatars';
$config['file_ext_tolower']     = TRUE;
$config['allowed_types']        = 'gif|jpg|jpeg|png';
$config['max_size']             = 1024;
$config['max_width']            = 1024;
$config['max_height']           = 1024;
$config['file_name']           = "Your Encrypted Name.ext";//add this to your code and try
$this->load->library('upload', $config);

最终版本如下所示:

在 Auth 的顶部 class 我添加了 $file_name 变量:

//upload file
public $file_name = null;

我创建了一个可重复使用的上传方法:

public function upload_avatar(){
    $config['upload_path'] = './assets/img/avatars';
    $config['file_ext_tolower']     = TRUE;
    $config['allowed_types']        = 'gif|jpg|jpeg|png';
    $config['max_size']             = 1024;
    $config['max_width']            = 1024;
    $config['max_height']           = 1024;
    $config['encrypt_name']         = TRUE;
    $this->upload->initialize($config); 

    if (!$this->upload->do_upload('userfile')){
        $error = array('error' => $this->upload->display_errors());
    } else {
        $this->file_name = $this->upload->data('file_name');
    }
}

我在 create_user()edit_user($id) 方法中使用了这个方法:

$this->upload_avatar();

然后我在$additional_data$data数组中添加头像,两种方法:

$additional_data = [
    //more code
    'avatar' =>  $this->file_name,
    //more code
];

$data = [
    //more code
    'avatar' =>  $this->file_name,
    //more code
]

查看 GitHub 提交 HERE

非常感谢所有提供帮助的人。