2013-03-09 169 views
1

我正在使用xampp在Win7 64上工作。 我正在上傳一個文件名爲希臘字符的文件。 文件名存儲錯誤 例如ελληνικά.xlsx存儲爲「上傳後文件名顯示不正確 - PHP

我想它必須做一些編碼。

在我的HTML我使用

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

下面

function handleFileSelect(evt) { 
    var output=[]; 

    if (evt.target.files.length === 0) exit(); 
    var f = evt.target.files[0]; 
    if ($.inArray(f.name.split('.').pop(), ['xls', 'xlsx']) === -1) { 
    $('#output').html('Wrong file type. Only Excel files are valid.'); 
    exit; 
    } 

    ans = fileunit(f.size);  
    output.push('<li><strong>', f.name, ' - ', 
       ans[0].toFixed(2), ans[1], '</li>'); 
    $('#list').html('<ul>' + output.join('') + '</ul>'); 

    var fd = new FormData(); 
    fd.append('file', evt.target.files[0]); 

    $.ajax({ 
    url: 'uploaddata/file', 
    data:fd, 
    processData: false, 
    contentType: false, 
    type: 'POST',  
    success: function(data){ 
     $('#output').html(data);}, 
    error: function(data) { 
     $('#output').html(data);}  
    }); 
}  

Server代碼我的JavaScript下面給出

public function upload() { 
// Checking upload error code 
    if ($_FILES["file"]["error"] > 0) 
    { 
     echo file_upload_error_message($_FILES["file"]["error"]); 
     return; 
    } 

    //Checking uplaod directory 
    $uploadDirectory = 'uploads' . DIRECTORY_SEPARATOR; 
    if(!is_dir($uploadDirectory)) 
    { 
     @mkdir ($uploadDirectory, 0766, true); 
    } 
    if(!is_dir($uploadDirectory)) 
    { 
     echo 'Server error. Impossible to create the upload folder.'; 
     return; 
    } 
    elseif(!is_writable($uploadDirectory)) 
    { 
     echo 'Server error. Upload directory is not writable.'; 
     return; 
    } 

    //Checking if file is too big 
    if ($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST) && 
     empty($_FILES) && $_SERVER['CONTENT_LENGTH'] > 0) 
    { 
     $error = 'Server error. File is too large, cannot upload.'; 
     echo $error; 
     return; 
    } 

    $file_name = $_FILES["file"]["name"]; 
    if (!move_uploaded_file($_FILES["file"]["tmp_name"], $uploadDirectory.$file_name)) 
    { 
     echo 'Server error. Error moving uploaded file from temp dir to upload dir'; 
     return; 
    } 
    echo $file_name . ' was uploaded successfully.'; 
    } 

給予我把一個斷點在PHP文件我查了
$ _FILES [file] [name]。
文件名似乎是正確的。同時文件內容上傳沒有錯誤。

我猜,當文件從臨時目錄移動到上傳目錄 有事情發生,但我的想法:(

+2

你不僅要驗證客戶端上,但特別是在服務器端用戶的輸入。目前,用戶可以上傳任何文件,包括PHP文件。 – Gumbo 2013-03-09 21:08:45

+0

@Gumbo感謝您的評論,這是一項正在進行的工作。所有用戶輸入將被服務器端驗證。 – 2013-03-10 04:50:13

回答

0

也許你需要urlencode()在服務器端的名稱,然後urldecode()爲顯示你的服務器是不處理Unicode的正確。

0

至於你說的那個文件名顯示正確(以希臘字母),您只需將它們轉換爲utf-8,你可以簡單地使用mb_convert_encoding

$file_name = mb_convert_encoding($_FILES[file][name], 'utf-8'); 
+0

我用mb_convert_encoding沒有運氣:( – 2013-03-10 18:48:33

2

我有與西班牙字符相同的問題。在我的情況下,我使用了utf8_decode()函數,它工作!

嘗試

$file_name = utf8_decode($file_name);