简体   繁体   English

jQuery文件上传 - 带有特殊字符的blueimp文件名

[英]jQuery File Upload - blueimp filenames with special characters

Some of my users will attach files with names that contain special characters which are problematic for the utility when downloading and uploading, single quotes, spaces, etc. This seems like it should be a simple problem to address but since I'm a "greenie" when coding I could use some help. 我的一些用户将附加名称包含特殊字符的文件,这些字符在下载和上传,单引号,空格等时对实用程序有问题。这似乎应该是一个简单的问题要解决,但因为我是“绿色” “编码时,我可以使用一些帮助。

I've tried modifying the 'UploadHandler.php' functions to manipulate the file name with no success. 我已经尝试修改'UploadHandler.php'函数来操作文件名但没有成功。 I'm not sure if I need to address it at the input level on the form or on the upload... 我不确定是否需要在表单或上传的输入级别处理它...

My last attempt was in the trim_file_name() function: 我的最后一次尝试是在trim_file_name()函数中:

protected function trim_file_name($name,
        $type = null, $index = null, $content_range = null) {
    // Remove path information and dots around the filename, to prevent uploading
    // into different directories or replacing hidden system files.
    // Also remove control characters and spaces (\x00..\x20) around the filename:
    $name = trim(basename(stripslashes($name)), ".\x00..\x20");
    // Use a timestamp for empty filenames:
    if (!$name) {
        $name = str_replace('.', '-', microtime(true));
        $name = preg_replace('/[^A-Za-z0-9\-]/', '', $name); <==== my attempt
    }
    // Add missing file extension for known image types:
    if (strpos($name, '.') === false &&
        preg_match('/^image\/(gif|jpe?g|png)/', $type, $matches)) {
        $name .= '.'.$matches[1];
    }
    return $name;
}

I could really use some help with this. 我真的可以使用一些帮助。 I'm really frustrated. 我真的很沮丧。

Thanks, 谢谢,

I use a couple of functions that have worked well in production to sanitise and clean file names, I'll put them below. 我使用了一些在生产中运行良好的函数来清理和清理文件名,我将它们放在下面。

public static function cleanFileName($filename)
{
    $filename = htmlentities($filename, ENT_QUOTES, 'UTF-8');
    $filename = preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', $filename);
    $filename = html_entity_decode($filename, ENT_QUOTES, 'UTF-8');
    $filename = preg_replace(array('~[^0-9a-z]~i', '~[ -]+~'), ' ', $filename);
    return trim($filename, ' -');
}

public static function sanitizeFileName($filename)
{
    $dangerous_characters = array(" ", '"', "'", "&", "/", "\\", "?", "#");
    return str_replace($dangerous_characters, '_', $filename);
}

So once you've copied both those functions to the UploadHandler class, the modified trim_file_name function would look like this: 因此,一旦将这两个函数复制到UploadHandler类,修改后的trim_file_name函数将如下所示:

protected function trim_file_name($name,
    $type = null, $index = null, $content_range = null) {
// Remove path information and dots around the filename, to prevent uploading
// into different directories or replacing hidden system files.
// Also remove control characters and spaces (\x00..\x20) around the filename:
$name = trim(basename(stripslashes($name)), ".\x00..\x20");

// Use a timestamp for empty filenames:
if (!$name) {
    $name = str_replace('.', '-', microtime(true));
}
// Add missing file extension for known image types:
if (strpos($name, '.') === false &&
    preg_match('/^image\/(gif|jpe?g|png)/', $type, $matches)) {
    $name .= '.'.$matches[1];
}
// Call sanitize file name function
$name = UploadHandler::sanitizeFileName($name);
// Call clean file name function
$name = UploadHandler::cleanFileName($name);
return $name;
}

Adam: "Extremely close! The filename is being modifed correctly but appears to adding the file extension as part of the file name, eg My'File.jpg = My File jpg.jpg Any ideas?" 亚当: “非常接近!文件名正在被正确修改,但似乎是将文件扩展名添加为文件名的一部分,例如My'File.jpg =我的文件jpg.jpg任何想法?”

I had same issue, i just fixed the function cleanFileName() ... you should add a dot "." 我有同样的问题,我只修复了函数cleanFileName() ...你应该添加一个点“。” to the last preg_replace function: 到最后一个preg_replace函数:

this line: 这一行:

$filename = preg_replace(array('~[^0-9a-z]~i', '~[ -]+~'), ' ', $filename);

should be: 应该:

$filename = preg_replace(array('~[^0-9.a-z]~i'),'',$filename);

so the dot is not removed by the regular expression. 所以正则表达式不会删除点。

public static function cleanFileName($filename)
{

    $filename = htmlentities($filename, ENT_QUOTES, 'UTF-8');
    $filename = preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', $filename);
    $filename = html_entity_decode($filename, ENT_QUOTES, 'UTF-8');
    $filename = preg_replace(array('~[^0-9.a-z]~i'),'',$filename);


    return trim($filename, ' -');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM