2015-04-25 77 views
2

我的一些用戶將附加名稱包含特殊字符的文件,這些文件在下載和上傳,單引號,空格等時對實用程序有問題。這看起來應該是一個簡單的問題來解決,但因爲我是一個「綠色」編碼時,我可以使用一些幫助。jQuery文件上傳 - 具有特殊字符的blueimp文件名

我試着修改'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; 
} 

我真的可以使用一些幫助。我真的很沮喪。

感謝,

+0

in wordpress,'sanitize_file_name()'很好。從wordpress/wp-includes/formatting.php複製這個函數並使用它 –

回答

2

我用一對夫婦的功能,在生產運作良好消毒和清潔的文件名,我將把它們放在下面。

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; 
} 
+0

所以我正確的在UploadHandler.php中實現這個功能嗎?這是否也會糾正fileupload.php頁面上的文件名,還是他們需要解決的問題? 謝謝, –

+0

@AdamM你可以在幾個地方調用這些函數。如果你想堅持你的行爲,在trim_file_name函數中調用它們。檢查我的編輯 – Muggles

+0

非常接近!文件名正在被正確地修改,但似乎將文件擴展名添加爲文件名的一部分,例如 My'File.jpg =我的檔案jpg.jpg 任何想法? –

0

亞當:「非常接近的文件名被正確體改但似乎將文件擴展名添加爲文件名的一部分,例如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, ' -'); 
}