你能建议/帮助我这个代码吗?我想将多个文件上传到服务器并将信息保存在数据库中。但是此代码仅从第一个输入字段上传一个文件。
我有动态的“上传文件”字段,如下所示:
<form action="uploader.php" method="POST" enctype="multipart/form-data">
<label>Upload</label>
<input type="file" name="file[]">
<label>Description</label>
<input type="text" id="filedesc" name="filedesc[]">
<a href="#" onClick="addUpload('dynamicInputUpload');" >
<img src="../../img/add.png" ></a>
<div id="dynamicInputUpload"></div>
<input type="submit" float= "right" name="add" id="add" value="UPLOAD" >
<input type="hidden" name="pid" id="pid" value="<?php echo $pid; ?>">
<input type="hidden" name="intnumber" id="intnumber" value="<?php echo $int; ?>">
</form>
Bellow 是创建动态字段的 Javascript:
var counterUpload = 1;
var limit = 10;
function addUpload(divName){
if (counterUpload == limit) {
alert("You have reached the limit of adding " + counterUpload + " inputs");
}
else {
var newdiv = document.createElement('div');
newdiv.innerHTML = " <label>File " + (counterUpload + 1) + "</label><input type=\"file\" name=\"file["+counterUpload+"]\" id=\"file["+counterUpload+"]\"> <label>Description " + (counterUpload + 1) + "</label><input type=\"text\" id=\"filedesc["+counterUpload+"]\" name=\"filedesc["+counterUpload+"]\" >"
document.getElementById(divName).appendChild(newdiv);
counterUpload++;
}
}
问题出在uploader.php 中。我成功地只上传了一个(第一个)文件。
<?php
include '../../c/config.php';
include '../../c/opendb.php';
// Settings
$allowedExtensions = array('jpg','gif','bmp','png', 'docx', 'doc','pdf');
$maxSize = 2097152;
$storageDir = '../uploads/';
// Result arrays
$errors = $output = array();
if (!empty($_FILES['file'])){
// Validation loop
foreach($_FILES['file']['name'] as $i=> $value){
$fileName = $_FILES['file']['name'][$i];
$fileSize = $_FILES['file']['size'][$i];
$fileErr = $_FILES['file']['error'][$i];
$fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
// Validate extension
if (!in_array($fileExt, $allowedExtensions)) {
$errors[$fileName][] = "Format $fileExt in file $fileName is not accepted";
}
// Validate size
if ($fileSize > $maxSize) {
$errors[$fileName][] = "$fileName excedes the maximum file size of $maxSize bytes";
}
// Check errors
if ($fileErr) {
$errors[$fileName][] = "$fileName uploaded with error code $fileErr";
}
}
// Handle validation errors here
if (count($errors) > 0) {
die("Errors validating uploads: ".print_r($errors, TRUE));
}
// Create the storage directory if it doesn't exist
if (!is_dir($storageDir)) {
if (!mkdir($storageDir, 0755, TRUE)) {
die("Unable to create storage directory $storageDir");
}
}
// File move loop
foreach($_FILES['file']['name'] as $i=> $array_value){
// Get base info
$fileBase = basename($_FILES['file']['name'][$i]);
$fileName = pathinfo($fileBase, PATHINFO_FILENAME);
$fileExt = pathinfo($fileBase, PATHINFO_EXTENSION);
$fileTmp = $_FILES['file']['tmp_name'][$i];
// Construct destination path
$fileDst = $storageDir.'/'.basename($_FILES['file']['name'][$i]);
for ($j = 0; file_exists($fileDst); $j++) {
$fileDst = "$storageDir/$fileName-$j.$fileExt";
}
// Move the file
if (move_uploaded_file($fileTmp, $fileDst)) {
$output[$fileBase] = "Stored $fileBase OK";
$sql=mysql_query("INSERT INTO uploaded_files (pid, file_path,file_type)
Values ('$_POST[pid]','$fileDst','$fileExt')")
or die ("Unable to issue query sql: ".mysql_error());
} else {
$output[$fileBase] = "Failure while uploading $fileBase!";
$errors[$fileBase][] = "Failure: Can't move uploaded file $fileBase!";
}
}
// Handle file move errors here
if (count($errors) > 0) {
die("Errors moving uploaded files: ".print_r($errors, TRUE));
} }