英文:
Sending POST request from JS to PHP - $_FILES is NULL, any ideas why?
问题
I am sending a POST request from a native JS file to a PHP file.
At some point, I was able to access my $_FILES var normally, without any problems; however, I added a second param (extension) and now it simply is NULL.
I was able to take the file from $_FILES and save it on my server.
var_dump($_POST) prints the [Object FormData], but var_dump($_FILES) prints NULL (not empty).
Any ideas? Thank you very much
the JS file :
const formData = new FormData();
formData.append("file", droppedFile);
// post request
const saveFile = await fetch("../controller/save-file.php", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
},
body: `formData=${formData}&extension=${droppedFile.name.split(".").pop()}`
}).then((response) => response.json());
the PHP file :
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: X-Requested-With');
ini_set('file_uploads', 'On');
ini_set('upload_max_filesize', '1GB');
ini_set('post_max_size', '1GB');
$path = "../models/";
if(isset($_POST)){
var_dump($_FILES); // PROBLEM HERE
// $location = $path . 'fbx/' .$filename;
// if(!file_exists($location)){
// if(!move_uploaded_file($_FILES['file']['tmp_name'], $location))
// {
// echo json_encode("File was not saved");
// exit(1);
// }
// echo json_encode("file was created");
// }
// else{
// echo json_encode("file already exists");
// }
}
else{
echo json_encode("no POST data");
}
英文:
I am sending a POST request from a native JS file to a PHP file.
At some point, I was able to access my $_FILES var normally, without any problems ; however, I added a second param (extension) and now it simply is NULL.
I was able to take the file from $_FILES and save it in my server.
var_dump($_POST) prints the [Object FormData], but var_dump($_FILES) prints NULL (not empty).
Any ideas? Thank you very much
the JS file :
const formData = new FormData();
formData.append("file", droppedFile);
//post request
const saveFile = await fetch("../controller/save-file.php", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
},
body: `formData=${formData}&extension=${droppedFile.name.split(".").pop()}`
}).then((response) => response.json());
the PHP file :
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: X-Requested-With');
ini_set('file_uploads', 'On');
ini_set('upload_max_filesize', '1GB');
ini_set('post_max_size', '1GB');
$path = "../models/";
if(isset($_POST)){
var_dump($_FILES); // PROBLEM HERE
// $location = $path . 'fbx/' .$filename;
// if(!file_exists($location)){
// if(!move_uploaded_file($_FILES['file']['tmp_name'], $location))
// {
// echo json_encode("File was not saved");
// exit(1);
// }
// echo json_encode("file was created");
// }
// else{
// echo json_encode("file already exists");
// }
}
else{
echo json_encode("no POST data");
}
答案1
得分: 0
尝试这样做:
const formData = new FormData();
formData.append("file", droppedFile);
formData.append("extension", ${droppedFile.name.split(".").pop());
// 发送 POST 请求
const saveFile = await fetch("../controller/save-file.php", {
method: "POST",
body: formData
}).then((response) => response.json());
$_POST['extension'] = ....
$_FILES = ....
英文:
Try this:
const formData = new FormData();
formData.append("file", droppedFile);
formData.append("extension",${droppedFile.name.split(".").pop());
//post request
const saveFile = await fetch("../controller/save-file.php", {
method: "POST",
body: formData
}).then((response) => response.json());
$_POST['extension'] = ....
$_FILES = ....
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论