无法读取 php 中的 FormData 对象
Can't read FormData object in php
我想使用 Ajax 功能并使用 formdata 对象在服务器上上传一个文件。单击按钮上传时,我显示了输入文件的所有值。
name: "Lich hoc.txt"
size: 1950
type: "text/plain"
但我的问题是我无法读取 php 代码中的对象,var_dump 什么也不是。
我使用了框架 Codeigniter 。
请帮我 !谢谢
HTML 代码
<form enctype="multipart/form-data" name="" id="" method="post" action="">
<input id="file" type="file" name="file" />
<button class="button" id="btnDangKi"> Đăng Kí </button>
</form>
JS代码
$(document).ready(function(){
$('#btnDangKi').click(function(){
UpLoadFile();
});
});
function UpLoadFile(){
var file_upload = document.getElementById("file").files[0];
var fd = new FormData();
fd.append("file",file_upload);
console.log(file_upload);
$.ajax({
url:'<?php echo base_url('QuanLyXe_Ajax/upload'); ?>',
data: fd,
processData: false,
contentType: false,
type: 'POST',
enctype: 'multipart/form-data',
mimeType: 'multipart/form-data',
success: function(data){
alert(data);
}
});
}
PHP 代码
public function UpLoad(){
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png|txt';
$this->load->library('upload', $config);
if ($this->upload->do_upload("file"))
{
echo "Thanh cong upload ";
}else
{
echo $this->upload->display_errors();
}
echo var_dump($_POST);
}
样式代码:)
<style>
#upload_button{
padding:5px 10px;
border:1px solid gray;
border-radius:10px;
cursor:hand;
cursor:pointer;
float:left;
margin-top:5px;
}
#upload_form{
float:left;
margin-right:10px;
width:300px;
padding:10px;
}
</style>
jQUERY 代码
<script>
$(document).ready(function(){
$('#upload_button').click(function(){
UpLoadFile();
});
});
function UpLoadFile(){
var formData = new FormData($("#form_file")[0]);
$.ajax({
url: "yukle.php",
type: "POST",
data: formData,
async: false,
success: function (cevap) {
$("#sonuc").append(cevap);
},
cache: false,
contentType: false,
processData: false
});
e.preventDefault();
}
</script>
HTML 代码
<div id="upload_form">
<form enctype="multipart/form-data" name="form_file" id="form_file" method="post" action="">
<input id="file" type="file" name="file" />
</form>
</div>
<div id="upload_button"> Upload</div>
PHP 代码
public function UpLoad($file){
if (
$file["type"]=="image/jpeg" or
$file["type"]=="image/png" or
$file["type"]=="image/gif"){
$dosya_adi=$file["name"];
$uzanti=substr($dosya_adi,-4,4);
$yeni_ad= './assets/uploads/'.$dosya_adi.$uzanti;
if (move_uploaded_file($file["tmp_name"],$yeni_ad)){
echo 'Files uploaded';
}else{
echo 'Upload Error';
}
}else{
echo 'Error';
}
}
UpLoad($_POST['file']);
我想使用 Ajax 功能并使用 formdata 对象在服务器上上传一个文件。单击按钮上传时,我显示了输入文件的所有值。
name: "Lich hoc.txt"
size: 1950
type: "text/plain"
但我的问题是我无法读取 php 代码中的对象,var_dump 什么也不是。 我使用了框架 Codeigniter 。 请帮我 !谢谢
HTML 代码
<form enctype="multipart/form-data" name="" id="" method="post" action="">
<input id="file" type="file" name="file" />
<button class="button" id="btnDangKi"> Đăng Kí </button>
</form>
JS代码
$(document).ready(function(){
$('#btnDangKi').click(function(){
UpLoadFile();
});
});
function UpLoadFile(){
var file_upload = document.getElementById("file").files[0];
var fd = new FormData();
fd.append("file",file_upload);
console.log(file_upload);
$.ajax({
url:'<?php echo base_url('QuanLyXe_Ajax/upload'); ?>',
data: fd,
processData: false,
contentType: false,
type: 'POST',
enctype: 'multipart/form-data',
mimeType: 'multipart/form-data',
success: function(data){
alert(data);
}
});
}
PHP 代码
public function UpLoad(){
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png|txt';
$this->load->library('upload', $config);
if ($this->upload->do_upload("file"))
{
echo "Thanh cong upload ";
}else
{
echo $this->upload->display_errors();
}
echo var_dump($_POST);
}
样式代码:)
<style>
#upload_button{
padding:5px 10px;
border:1px solid gray;
border-radius:10px;
cursor:hand;
cursor:pointer;
float:left;
margin-top:5px;
}
#upload_form{
float:left;
margin-right:10px;
width:300px;
padding:10px;
}
</style>
jQUERY 代码
<script>
$(document).ready(function(){
$('#upload_button').click(function(){
UpLoadFile();
});
});
function UpLoadFile(){
var formData = new FormData($("#form_file")[0]);
$.ajax({
url: "yukle.php",
type: "POST",
data: formData,
async: false,
success: function (cevap) {
$("#sonuc").append(cevap);
},
cache: false,
contentType: false,
processData: false
});
e.preventDefault();
}
</script>
HTML 代码
<div id="upload_form">
<form enctype="multipart/form-data" name="form_file" id="form_file" method="post" action="">
<input id="file" type="file" name="file" />
</form>
</div>
<div id="upload_button"> Upload</div>
PHP 代码
public function UpLoad($file){
if (
$file["type"]=="image/jpeg" or
$file["type"]=="image/png" or
$file["type"]=="image/gif"){
$dosya_adi=$file["name"];
$uzanti=substr($dosya_adi,-4,4);
$yeni_ad= './assets/uploads/'.$dosya_adi.$uzanti;
if (move_uploaded_file($file["tmp_name"],$yeni_ad)){
echo 'Files uploaded';
}else{
echo 'Upload Error';
}
}else{
echo 'Error';
}
}
UpLoad($_POST['file']);