php - 如何遍历数组以获取多个要上传的文件
问题描述
我遇到了一种情况,我无法上传多个文件。如果我添加多个文件,则只有最后一个文件会发送。
我知道需要发送一个数组然后循环遍历,所以我将输入的名称更改为包含[]
,然后尝试更改public function upload
. 下面您将看到 fileUpload 类的原始版本,然后是我尝试的更新版本。
这是我移动文件的部分代码的原始版本:
class fileUpload
{
public function __construct()
{}
public function upload() {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["uploadedFile"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
return 0;
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["uploadedFile"]["tmp_name"], $target_file)) {
return basename($_FILES["uploadedFile"]["name"]);
} else {
return 0;
}
}
}
}
这是 fileUpload.php 的更新版本,试图循环遍历数组。
class fileUpload
{
public function __construct()
{}
public function upload(&$file_post) {
$file_ary = array();
$file_count = count($file_post['name']);
$file_keys = array_keys($file_post);
for ($index = 0; $index<$file_count; $index++) {
foreach ($file_keys as $key) {
$file_ary[$index][$key] = $file_post[$key][$index];
}
}
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["uploadedFile"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
return 0;
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["uploadedFile"]["tmp_name"], $target_file)) {
return basename($_FILES["uploadedFile"]["name"]);
} else {
return 0;
}
}
}
}
这样做,我收到以下错误:
注意:第30行/php/sharePhotoSend.php中的数组到字符串转换 致命错误:未捕获的 ArgumentCountError:函数 fileUpload::upload() 的参数太少,第 73 行的 /php/sharePhotoSend.php 中传递了 0 并且预期正好为 1在 /php/fileUpload.php:8 堆栈跟踪:
0 /php/sharePhotoSend.php(73): 文件上传->上传()
1 {main}在第8行的/php/fileUpload.php中抛出
以下是引用的行:
fileUpload.php 第 8 行 -public function upload(&$file_post) {
sharePhotoSend.php 第 30 行 -$projectSubmission_stmt->execute(array($first_name, $last_name, $email, $phone, $company, $details, $fileNameInsert));
第 73 行 -$filename = $fu->upload();
第 73 行是其中的一部分:
if (!empty($_FILES['uploadedFile']['name']) && $_FILES['uploadedFile']['error'] != 4) {
$fu = new fileUpload();
$filename = $fu->upload();
$template = str_replace("{filename}", "A file was uploaded. You can download the file from: <a href='https://mbkit.com/php/uploads/{$filename}'>{$filename}</a>", $template);
// print_r($template);
// echo "Should be working and hitting the if condition";
if ( !$filename ) {
echo json_encode(['status_code' => 500,
'message' => "We were not able to upload your file at this time."]);
}
clearstatcache();
更新 - 新的 fileUpload 类:
<?php
class fileUpload
{
public function __construct()
{}
public function upload() {
$file_ary = array();
$file_count = count($_FILES['uploadedFile']['name']);
//$file_count = count($_FILES($file_post['name']));
for ($i = 0; $i<$file_count; $i++) {
// echo $file['uploadedFile']['name'][$index] . "\n";
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["uploadedFile"]["name"][$i]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
return 0;
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["uploadedFile"]["tmp_name"][$i], $target_file)) {
return basename($_FILES["uploadedFile"]["name"][$i]);
} else {
return 0;
}
}
}
}
}
Communication Class - this sends the attachments
if (!empty($file) && !$recipient && count($file['uploadedFile']['name']) > 1) {
$f = new ZipArchive();
$zip = $f->open('uploads/' . $file['uploadedFile']['name'][0] . ".zip", ZipArchive::CREATE | ZipArchive::OVERWRITE);
if ($zip) {
for ($index = 0; $index < count($file['uploadedFile']['name']); $index++) {
// echo $file['uploadedFile']['name'][$index] . "\n";
$f->addFile($file['uploadedFile']['tmp_name'][$index], $file['uploadedFile']['name'][$index]);
}
$f->close();
$message["attachment[0]"] = curl_file_create("uploads/{$file['uploadedFile']['name'][0]}.zip",
pathinfo("uploads/{$file['uploadedFile']['name'][0]}.zip", PATHINFO_EXTENSION),
$file['uploadedFile']['name'][0] . ".zip");
} else {
throw new Exception("Could not zip the files.");
}
}
解决方案
推荐阅读
- angular - MEAN 中的第 3 方 API 调用
- python - 将谷歌数据存储和大查询数据连接在一起
- javascript - reactjs中设置时间间隔的问题
- swift - 更改约束的高度值时出错
- kubernetes - 在牧场主的私有目录中,没有显示模板文件
- email - 用于电子邮件的 amp 中的 amp-list 组件错误
- reactjs - 将 styled-components 导入组件 React 时出错
- mysql - SELECT 子句中的 MySQL 相关子查询
- sql - 分别找出 2019 年新加入者人数最多和最少的月份
- regex - 正则表达式在精确给定的函数中提取文本