簡體   English   中英

具有動態字段的多文件上傳

[英]Multiple File upload with dynamic fields

您能否提供有關此代碼的建議/幫助我? 我想將多個文件上傳到服務器並將信息保存在數據庫中。 但是此代碼僅從第一個輸入字段上載一個文件。

我有動態的“上傳文件”字段,如下所示:

              <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>    

波紋管是創建動態字段的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));
} }

該字符串有錯誤:

$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());

嘗試注釋該字符串,您的腳本將保存所有文件。 也許您沒有連接到數據庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM