为什么在此Codeigniter 3应用程序中上传之前重命名图像文件失败? [英] Why does renaming an image file before upload fail in this Codeigniter 3 application?

查看:70
本文介绍了为什么在此Codeigniter 3应用程序中上传之前重命名图像文件失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Codeigniter 3开发一个社交网络应用程序, Ion-Auth 和Bootstrap4。您可以看到 Github repo HERE

I am working on a Social Network application with 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 <之前正确地对文件名进行了哈希处理/ code>列,但上传本身从未发生。

The code above correctly hashes the filename before inserting it in the avatarcolumn, but the upload itself never happens.

我正在使用下面的代码:

I am uploading a user image (avatar) with the code below:

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块中看到的那样,我通过将当前文件名添加到当前文件名来更改它时间戳和哈希值。

As you can see in the else block, I am changing the original filename by adding it the current timestamp and hashing.

问题在于图像文件本身在上传之前未相应重命名。它以原始名称上传(图像未存储在 / assets / img / avatars / 中)。

The problem is that the image file itself is not renamed accordingly before the upload. It is uploaded with the original name (the image is not stored in /assets/img/avatars/).

为什么这样做

推荐答案

如果使用 UPLOAD

$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/";

这篇关于为什么在此Codeigniter 3应用程序中上传之前重命名图像文件失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆