我有一个包含多个字段的表单,其中之一是register_id。我还有一个上传按钮,可以通过模式按钮上传多个图像。用户按下提交后,我将收到一个ajax调用,它同时发送register_id和表单数据。但是,我在访问表单数据时遇到问题。
这是我的javascript,当用户在他们上载图像的模式窗口中按“提交”时执行:
$("form").on("submit", function(e) {
e.preventDefault();
var regId = (document.getElementById('register_id').value);
var formdata = new FormData(this);
var postData = { formdata: formdata, regId: regId };
var dataString = JSON.stringify(postData);
$.ajax({
url: 'uploadImages.php',
type: 'POST',
data: {myData:dataString},
dataType: "json",
success: function(response) {
document.getElementById("body").innerHTML = "<p>"+response+"</p>";
},
});
});
在我的PHP中,我想根据传递的注册ID创建一个自定义文件夹并获取所有图像。我正在设法回显寄存器ID,但无法获取FormData。回声$obj->formdata;
后,我得到:
jquery.min.js:2 POST http://localhost/CanvasCreator/uploadImages.php 500(内部服务器错误)
发送@ jquery.min.js:2
ajax @ jquery.min.js:2
(匿名)@ uploadImages.js:7 派遣@ jquery.min.js:2 v.handle @ jquery.min.js:2
这是我的php代码:
<?php
$obj = json_decode($_POST["myData"]);
$regNo = $obj->regId;
$formData = $obj->formdata;
$all_files = $formData;
echo($formData);
echo($regNo);
$path = 'SourceImages/'.$regNo.'/';
mkdir($path, 0777, true);
$extensions = ['tif'];
$all_files = count($_FILES['file']['name']);
for ($i = 0; $i < $all_files; $i++) {
$uploadOk = "";
$file_name = $_FILES['file']['name'][$i];
$file_tmp = $_FILES['file']['tmp_name'][$i];
$file_type = $_FILES['file']['type'][$i];
$file_size = $_FILES['file']['size'][$i];
$file_ext = strtolower(end(explode('.', $_FILES['file']['name'][$i])));
$file = $path . $file_name;
if (!in_array($file_ext, $extensions)) {
$uploadOk = $uploadOk . 'Extension not allowed: ' . $file_name. '</br>';
}
if ($file_size > 2097152) {
$uploadOk = $uploadOk . 'File size exceeds limit: ' . $file_name. '</br>';
}
if (file_exists($file)) {
$uploadOk = $uploadOk . 'File already exists: ' . $file_name. '</br>';
}
if ($file_size <1) {
$uploadOk ='Choose at least one image file' . $file_name. '</br>';
}
if($uploadOk != ""){
echo $uploadOk;
}else{
if(move_uploaded_file($file_tmp,$file)){
echo "File saved: ".$file_name."</br>";
}else{
echo "File not saved: ".$file_name."</br>";
}
}
}