你能告诉/帮我这个代码吗?我想将多个文件上传到服务器并将信息保存在数据库中。但是这段代码只从第一个输入字段上传一个文件。
我有动态“上传文件”字段,如下所示:
<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));
} }
答案 0 :(得分:0)
此字符串有错误:
$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());
尝试对此字符串发表评论,您的脚本将保存所有文件。 也许你没有连接到数据库。