2014-05-24 33 views
0

我試圖使用AJAX上傳多個圖像和排序數據,如下面的數據結構。

我的問題是使用AJAX來獲取$_FILES['tmp_name']加入到客戶端的JSON數據, (該$_FILES['tmp_name']被選擇的文件上傳輸入,存儲文件到臨時位置後)
這是任何安全問題,我應該注意?

PHP存儲功能只獲取數據並將$_FILES['tmp_name']移動到其他位置而沒有任何驗證。但tmp_name路徑是從客戶端是它太危險,不應該這樣做?

數據結構

post_array = { 
    "gallery": [ 
     0: ['file input val', 'file sequence', 'file type', 'tmp_name'], 
     1: ['file input val', 'file sequence', 'file type', 'tmp_name'], 
     ... 
    ] 
}; 

的Javascript:

var form_data = new FormData(), 
    post_array = {}, 
    files = []; 

for (var i = 0; i < $('.file-list').length; i++) { 

    // ... ajax get $_FILES['tmp_name'], tmp_name = $_FILES['tmp_name'] 

    var file = []; 
    file.push($('.file-list').eq(i).find('.browseimage')[0].files[0]); 
    file.push(tmp_name); 
    file.push($('.file-list').eq(i).find('.file-sequence input').val()); 
    file.push($('.file-list').eq(i).find('.file-type input').val()); 

    files.push(file); 
} 

post_array['gallery'] = files; 

var json_array = JSON.stringify(post_array); 

form_data.append('post_array', json_array); 

// ... ajax post form_data store 

PHP店

$post_array = json_decode($_POST['post_array'], true); 
// ... 
for ($i=0; $i < count($gallery); $i++) { 
    $tmp_name = $gallery[$i][0]['name']; 
    $tmp_size = $gallery[$i][0]['size']; 
    $tmp_format = $gallery[$i][0]['type']; 
    $tmp_location = $gallery[$i][0]['tmp_name']; 
    ... 

    move_uploaded_file($tmp_location, $file_correct_location); 

回答

-1

你一定要讀一些信息的瞭解,因爲有你有幾個安全注意事項意識到。

要回答你的問題: 不,你不應該那樣做,因爲用戶給出的路徑導致目錄結構中上一級文件夾的風險很高。此外,該文件可能是病毒或在服務器上執行的腳本。

安全考慮的良好起點: http://www.mysql-apache-php.com/fileupload-security.htm

相關問題