[英]jQuery File Upload - blueimp filenames with special characters
我的一些用戶將附加名稱包含特殊字符的文件,這些字符在下載和上傳,單引號,空格等時對實用程序有問題。這似乎應該是一個簡單的問題要解決,但因為我是“綠色” “編碼時,我可以使用一些幫助。
我已經嘗試修改'UploadHandler.php'函數來操作文件名但沒有成功。 我不確定是否需要在表單或上傳的輸入級別處理它...
我的最后一次嘗試是在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;
}
我真的可以使用一些幫助。 我真的很沮喪。
謝謝,
我使用了一些在生產中運行良好的函數來清理和清理文件名,我將它們放在下面。
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);
}
因此,一旦將這兩個函數復制到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;
}
亞當: “非常接近!文件名正在被正確修改,但似乎是將文件擴展名添加為文件名的一部分,例如My'File.jpg =我的文件jpg.jpg任何想法?”
我有同樣的問題,我只修復了函數cleanFileName()
...你應該添加一個點“。” 到最后一個preg_replace
函數:
這一行:
$filename = preg_replace(array('~[^0-9a-z]~i', '~[ -]+~'), ' ', $filename);
應該:
$filename = preg_replace(array('~[^0-9.a-z]~i'),'',$filename);
所以正則表達式不會刪除點。
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.