【问题标题】:PHP not reading my files sent by ajax with JQueryPHP 没有使用 JQuery 读取我的 ajax 发送的文件
【发布时间】:2015-01-28 21:03:04
【问题描述】:

所以我有这个问题,我不可能通过 AJAX/JQuery 读取/保存文件,我正在使用这个发送

<form class='file'>
	<input type='file' class='file_upload' name='def' required/>
	<input type='submit' value="Wyślij">
</form>

这是我对发送 JQuery 内容的尝试:

$(document).ready(function(){
	$("form.file").unbind().on("submit",function(event){
		$(this).find('input[type=submit]').attr('disabled', 'disabled');
		event.preventDefault();
		$(this).find(".file_upload").each(function(){
			sendFile(this.files[0]);
		});
		
		$(this).parent().remove();
	});
});


function sendFile(file){
	$.ajax({
		type: 'post',
		url:'sayv.php?name='+file.name,
		data:file,
		processData: false,
    	contentType: file.type,
		success: function(data){
			console.log(data);
		}
			});
}

而且,我无论如何都无法通过 PHP 读取它, $_FILES 为空,$_POST 也是...

【问题讨论】:

标签: jquery forms file post


【解决方案1】:

由于您发送的是未包装在表单数据对象中的裸文件,因此它将成为请求正文。使用file_get_contents('php://input');阅读。

file_put_contents('filename.ext', file_get_contents('php://input'));

【讨论】:

    猜你喜欢
    • 2014-04-16
    • 2021-05-24
    • 2016-02-05
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多