[英]How to get an array of files to upload
I am running into a scenario that I cannot get more than one file to upload. 我遇到无法上传多个文件的情况。 If I add more than one file, only the last one will send through.
如果我添加多个文件,则只有最后一个文件会发送出去。 I know this means I need to put the files into an array, but when I try I get an error from the fileUpload class.
我知道这意味着我需要将文件放入数组,但是当我尝试时,我从fileUpload类中得到了一个错误。
IF I change the input html's name to `name="uploadedFile[]"' then I get the following error in my fileUpload class: 如果我将输入的html的名称更改为`name =“ uploadedFile []”',则在我的fileUpload类中收到以下错误:
PHP Warning: basename() expects parameter 1 to be string, array given in /php/fileUpload.php on line 11
PHP警告:basename()期望参数1为字符串,在第11行的/php/fileUpload.php中给出的数组
PHP Warning: move_uploaded_file() expects parameter 1 to be string, array given in /php/fileUpload.php on line 46
PHP警告:move_uploaded_file()期望参数1为字符串,在第46行的/php/fileUpload.php中给出的数组
Then in my Communication class, you will see that if there is more than one file being uploaded, I send it as a zip, so I'm not sure if anything would need to be changed in there. 然后,在Communication类中,您将看到,如果上传的文件不止一个,我会以zip格式发送该文件,因此不确定是否需要在其中进行任何更改。
Does anyone see what I need to do to get the array to work? 有人看到我需要做些什么才能使阵列工作吗?
HTML: HTML:
<input type="file" name="uploadedFile" class="inputfile" id="uploadedFileTest" data-multiple-caption="{count} files selected" multiple>
FileUpload class: 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;
}
}
}
}
Rest of the php file: 其余的php文件:
$files = null;
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);
clearstatcache();
}
Communication Class - what sends out the attachment(s): 通信类-发送附件的内容:
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.");
}
// $message['html'] = str_replace("{filename}", "A file was uploaded. You can download the file from: <a href='https://mbkit.com/php/uploads/{$file['uploadedFile']['name']}.zip'>{$file['uploadedFile']['name']}</a>", $message['html']);
} elseif (count($file['uploadedFile']['name']) == 1) {
$message["attachment[0]"] = curl_file_create("uploads/{$file['uploadedFile']['name']}",
pathinfo("uploads/{$file['uploadedFile']['name']}", PATHINFO_EXTENSION),
$file['uploadedFile']['name']);
}
Try inspecting the $_FILES array to see the structure of a single file upload and a mulit-file upload. 尝试检查$ _FILES数组以查看单个文件上传和多文件上传的结构。
Here is a small function you can use to visually inspect an array: 这是一个小函数,可用于直观地检查数组:
function varDumpToString($var, $type=false){
ob_start();
var_export($var);
$return = ob_get_clean();
return ($type === 'web'
? str_replace("\n", '<br/>', $return)
: $return
);
}
Usage: 用法:
echo varDumpToString($_FILES, 'web');
Alternatively: 或者:
error_log(varDumpToString($_FILES));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.