【发布时间】:2021-08-28 21:25:22
【问题描述】:
尝试在不使用表单的情况下上传文件并使用$.post 传输文件
我想问题出在php方面,但我不确定
<input type='file' id='inpfile'>
$(inpfile).on('change', function(){
var fd = new FormData();
var file = $(inpfile)[0].files[0];
fd.append('file', file);
fd = JSON.stringify(fd);
$.post('pro.php', {fn: 'upload', args: [fd]}, function(data){
console.log(data);
});
});
pro.php
if(isset($_POST['fn'], $_POST['args'])){
$fn = $_POST['fn']; $args = $_POST['args'];
$fn(...$args);
}
function upload($fd){
$fd = json_decode($fd);
$fname = $fd->file;
$destination = 'upload/' . $fname;
move_uploaded_file($fname, $destination);
}
【问题讨论】:
-
你的代码是不安全的,从任意文件上传到任意函数执行,你需要保护
fn: 'upload'免受fn: 'exec', args:['rm . -Rf']或fn: 'mail', args:['spam@example.com', 'subject', 'message']等的影响
标签: javascript php jquery ajax upload