5 回答

TA贡献1809条经验 获得超8个赞
<?php
// Count total files
var_dump($_FILES['files']);
$countfiles = count($_FILES['files']['name']);
echo $countfiles;
// Upload directory
$upload_location = "uploads/";
// To store uploaded files path
$files_arr = array();
// Loop all files
for($index = 0;$index < $countfiles;$index++){
echo $index;
// File name
$filename = $_FILES['files']['name'][$index];
echo $filename;
// Get extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
echo $ext;
// Valid image extension
$valid_ext = array("png","jpeg","jpg", "csv");
// Check extension
if(in_array($ext, $valid_ext)){
// File path
$path = $upload_location.$filename;
// Upload file
if(move_uploaded_file($_FILES['files']['tmp_name'][$index],$path)){
$files_arr[] = $path;
}
}
}
/* echo json_encode($files_arr); */
die;
const Axios = require('axios');
const Fs = require('fs');
const FormData = require('form-data');
const allFiles = [
{uri: 'my/Spark_Dataset/combin.csv'},
{uri: 'my/Spark_Dataset/combine-csv.csv'},
// {uri: 'C:\\Users\\my\\combine-csv.csv'}
];
(async function() {
var formData = new FormData();
//here allFiles is an array of object , each object contains 1 file with details like local file path ,name,size
for (var index = 0; index < allFiles.length; index++) { //Append multiple files to formData.
// console.log(index);
let currentFile = allFiles[index]
//createReadStream Retyrns fileData
// console.log(currentFile);
let fileWithInfo = await Fs.createReadStream(currentFile.uri)
formData.append("files[]", fileWithInfo );
}
console.log(formData);
Axios.post('http://localhost:8000/upload.php',
formData,
{
headers: formData.getHeaders(),
// 'Content-Type':'multipart/form-data',
})
.then(d => console.log("DONE=>>>> ", d.data))
.catch((f) => console.log('FAILED=>> ', f))
})();
packages.json
"axios": "^0.20.0",
"form-data": "^3.0.0",
"fs": "0.0.1-security",
首先我启动了本地服务器php -S localhost:8000
运行本地服务器后,我运行 index.js 文件,node index.js
我在本地测试了代码,它工作正常。

TA贡献1966条经验 获得超4个赞
Axios.post('example.com/upload.php', formData, {
headers: formData.getHeaders()
})
.then(d => console.log("DONE=>>>> ", d.data))
.catch((f) => console.log('FAILED=>> ', f));
您缺少axios post 请求中您的情况,您可以直接传递 formData 或调用 formData.getBuffer() data。formData

TA贡献1810条经验 获得超4个赞
https://github.com/form-data/form-data#buffer-getbuffer
const { readFile } = require('fs/promises');
for (let index = 0; index < allFiles.length; index++) {
const currentFile = allFiles[index];
const fileWithInfo = await readFile(currentFile.uri);
formData.append('files[]', fileWithInfo);
}
axios.post('https://example.com/', formData.getBuffer(), formData.getHeaders());
// you have to send files as buffer
还有流 https://stackoverflow.com/a/53126159/8784402的解决方案
const response = await axios({
method: 'post',
url: 'http://www.yourserver.com/upload',
data: formData,
headers: {
'content-type': `multipart/form-data; boundary=${form._boundary}`,
},
});

TA贡献1818条经验 获得超3个赞
也许我的回答没有解决,但我建议对 PHP 代码进行一些更改。当然,$_FILES 数组中的键不能是数字,最好使用 foreach 而不是 for。我允许自己稍微更改您的代码以使其更小
<?php
// Upload directory
$upload_location = "uploads/";
// To store uploaded files path
$files_arr = array();
// Valid image extension
$valid_ext = array("png","jpeg","jpg");
foreach($_FILES['files']['name'] as $key => $file) {
if(
in_array(pathinfo($_FILES['files']['name'][$key], PATHINFO_EXTENSION), $valid_ext)
&&
move_uploaded_file($_FILES['files']['tmp_name'][$key],$upload_location.$_FILES['files']['name'][$key])
) {
$files_arr[] = $upload_location.$_FILES['files']['name'][$key];
} else {
//Only for debugging
$files_arr[] = 'Not uploaded: '.$upload_location.$_FILES['files']['name'][$key];
}
}
echo json_encode($files_arr);
die;

TA贡献2036条经验 获得超8个赞
您allFiles.length
在 NodeJS 代码中错过了循环中的参数。正确的代码将是
for (var index = 0; index < allFiles.length; index++) {
PHP代码是正确的,但是如果你发送一个空的files数组,它不会出错并且会返回成功响应!
添加回答
举报