gpt4 book ai didi

php - 使用多文件 uploader 只上传一个文件

转载 作者:行者123 更新时间:2023-12-04 13:42:09 27 4
gpt4 key购买 nike

下面的 PHP 文件脚本只上传一个文件,尽管它应该发送多个文件。看不懂哪里断了。尝试使用 foreach 但无济于事。

我做了一个var_dump,它们显示了正确的发送文件数。

HTML

<form id="fileupload" method="POST" enctype="multipart/form-data">
<input type="file" name="uploadfile[]" multiple id="uploadfile" />
</form>

PHP

<?php

require_once 'config.php';

############ Edit settings ##############
$UploadDirectory = './storage/';
#########################################


if(!isset($_FILES["uploadfile"])) {
die('Something wrong with upload! Is "upload_max_filesize" set correctly?');
}

//check if this is an ajax request
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])){
die();
}


// Total number of files to be uploaded
$total_files = count($_FILES['uploadfile']['name']);

//check if there is at least 1 file
if($total_files < 1) {
die();
}


for($i=0; $i<$total_files; $i++) {
//Is file size is less than allowed size.
if ($_FILES["uploadfile"]["size"][$i] > 29000000) {
die("File size is too big!");
}

$allowedTypes = array('gif', 'png', 'jpeg', 'jpg', 'pdf', 'xls', 'xlsx', 'doc', 'docx', 'ppt', 'pptx', 'mp3', 'mp4', 'rar', 'zip', 'txt');

$FileNameFull = strtolower($_FILES['uploadfile']['name'][$i]);
$FileNameShort = pathinfo($FileNameFull, PATHINFO_FILENAME);
$FileExt = pathinfo($FileNameFull, PATHINFO_EXTENSION);

if(!in_array($FileExt, $allowedTypes)) {
die('Unsupported File format!');
}

$FileCode = rand(0, 9999999999); //Random number to be used to rename actual filename
$NewFileName = $FileCode.'.'.$FileExt; //new file name

if(move_uploaded_file($_FILES['uploadfile']['tmp_name'][$i], $UploadDirectory.$NewFileName )) {
// Save the file details to database
//$query = $dbh->prepare("INSERT INTO uploads(file_name, file_code, file_ext, timestamps) VALUES (:file_name, :file_code, :file_ext, :timestamps)");
/*$query->execute(array(
":file_name" => $FileNameShort,
":file_code" => $FileCode,
":file_ext" => $FileExt,
":timestamps" => round(microtime(true) * 1000)
));*/

die('Success! File Uploaded.');
}
else{
die('error uploading File!');
}

}

最佳答案

好吧,我给你举个小例子:

HTML:

<input name="uploadfile[]" type="file" multiple="multiple" />

PHP:

$totalImage = count($_FILES['uploadfile']['name']);

for($i=0; $i<$totalImage; $i++) {
$temporaryPathOfImage = $_FILES['uploadfile']['tmp_name'][$i];

if ($temporaryPathOfImage != ""){
$dirPath = "./storage/" . $_FILES['uploadfile']['name'][$i];

if(move_uploaded_file($temporaryPathOfImage, $dirPath)) {
//Code Here
}
}
}

问题:

die('Success! File Uploaded.');

删除此行,以便您将获得该循环中的所有图像。

关于php - 使用多文件 uploader 只上传一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39387694/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com