![](/img/trans.png)
[英]Is it possible to get $_FILES[“inputID”][“tmp_name”] from a file field using javascript?
[英]Get $_FILES['tmp_name'] to JSON data and resent
我嘗試使用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);
您絕對應該閱讀一些有關此的信息,因為您必須注意一些安全注意事項。
要回答您的問題:不,您不應該那樣做,因為用戶提供的路徑很可能導致目錄結構中的目錄向上一級。 此外,該文件可能是病毒或在服務器上執行的腳本。
安全注意事項的一個很好的起點: http : //www.mysql-apache-php.com/fileupload-security.htm
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.