do_upload() 函数从 Codeigniter 3 中的文件名中删除“&”字符

问题描述 投票:0回答:1

我在 CI3 中使用 do_upload() 函数来重命名和上传文件。文件名包含“&”字符。当我上传文件并保存它时,文件名中的“&”字符被删除。下面是代码-

// Upload File Name : ***Heavy & Light Vehicles.csv***

// File post parameter : *im_file*


    $config['file_name'] = $saved_file_name = uniqid() . '_' . $_FILES['im_file']['name'];
    $config['allowed_types'] = 'csv';
    $config['overwrite'] = TRUE;
    $this->load->library('upload');
    $this->upload->initialize($config);
    if (!$this->upload->do_upload('im_file')) {
         $error = array('error' => $this->upload->display_errors());
         $im_file = "";
         exit();
    } else {
         $filedata = array('upload_data' => $this->upload->data());
         print_r($filedata);
    }

打印的数组如下 -

[upload_data] => Array
        (
            [file_name] => 65e5c1262de2d_Heavy_Light_Vehicles.csv
            [file_type] => text/plain
            [file_path] => D:/wamp/www/project_folder/uploads/import_file/vehicles/
            [full_path] => D:/wamp/www/project_folder/uploads/import_file/vehicles/65e5c1262de2d_Heavy_Light_Vehicles.csv
            [raw_name] => 65e5c1262de2d_Heavy_Light_Vehicles.csv
            [orig_name] => 65e5c1262de2d_Heavy_Light_Vehicles.csv
            [client_name] => Heavy & Light Vehicles.csv
            [file_ext] => .csv
            [file_size] => 99.2
            [is_image] => 
            [image_width] => 
            [image_height] => 
            [image_type] => 
            [image_size_str] => 
        )

在上面打印的“文件上传”数组中,文件名中的“&”字符在保存时被删除。保存后的预期文件名应为65e5c1262de2d_Heavy_&_Light_Vehicles.csv。请建议是否有任何解决方案可以保持文件名“&”字符不变。

php codeigniter-3
1个回答
0
投票

do_upload
方法使用
sanitize_filename
类中的
CI_Security
方法从文件名中删除同一类中公共
$filename_bad_chars
数组中列出的所有字符。

要保留

&
,您可以扩展
CI_Security
类,并使用删除了
$filename_bad_chars
的原始数组的副本覆盖公共
&
数组。

如果将类命名为

MY_Security.php
并将其保存在
application/core
文件夹中,CodeIgniter 将自动使用这个新类:

<?php

class MY_Security extends CI_Security
{

    /**
     * List of sanitize filename strings
     *
     * @var array
     */
    public $filename_bad_chars =    array(
        '../', '<!--', '-->', '<', '>',
        "'", '"', '$', '#',
        '{', '}', '[', ']', '=',
        ';', '?', '%20', '%22',
        '%3c',        // <
        '%253c',    // <
        '%3e',        // >
        '%0e',        // >
        '%28',        // (
        '%29',        // )
        '%2528',    // (
        '%26',        // &
        '%24',        // $
        '%3f',        // ?
        '%3b',        // ;
        '%3d'        // =
    );

    public function __construct()
    {
        parent::__construct();
    }
}

另请参阅:https://codeigniter.com/userguide3/general/core_classes.html#extending-core-class

© www.soinside.com 2019 - 2024. All rights reserved.