1

Bootstrap FileInput插件有两个问题:

  1. 我正在上传到“临时”文件夹以存放文件,直到提交表单并且我得到一个唯一的密钥,我可以将附件和表单数据关联到该密钥。现在的问题是,如果用户想通过本机“垃圾箱”图标删除上传的文件,则上传的文件仍保留在“temp”文件夹中,因为未定义删除功能。如何设置“删除附件”按钮并将其指向“临时”文件夹中的附件?

  2. 我有一个带有 2 个选项卡的网页,每个选项卡都包含一个供用户填写的表格。我需要每个表单来显示文件输入插件,但是在复制代码后(即使输入字段有不同的名称),只有第一个文件输入字段可以正常工作,第二个根本不上传文件。如何在一个网页上运行多个插件实例?

输入字段+JS:

<h4>Attachments</h4>
<div class="col-md-12">

    <input tabindex="19" id="input" name="input[]" type="file" multiple class="file-loading">
    <p class="help-block">Max. filesize 5MB, images only (.jpg || .png || .bmp)</p>
    <script>
    var $input = $("#input");
    $input.fileinput({
        uploadUrl: "./attachments/upload.php", // server upload action
        uploadExtraData: {log:'auto',holidex:'<?php echo $_SESSION['Holidex']; ?>',user:'<?php echo $_SESSION['myusername']; ?>'},
        uploadAsync: false,
        showUpload: false, // hide upload button
        showRemove: false, // hide remove button
        maxFileCount: 10,
        'maxFileSize': 5120,
        allowedFileTypes: ["image", "video"]
    }).on("filebatchselected", function(event, files) {
        // trigger upload method immediately after files are selected
        $input.fileinput("upload");
    });
    </script>

</div>

上传.php

<?php
session_start();

if(isset($_FILES['input'])) {

    // define variables
    $holidex = $_POST['holidex'];
    $user = $_POST['user'];
    $output_dir = "./".$holidex."/temp/".$user."/";

    // check whether temporary folder exists, otherwise create
    if (!file_exists($output_dir)) {
        mkdir($output_dir, 0755, true);
    }

    $ret = array();

//  This is for custom errors;  
/*  $custom_error= array();
    $custom_error['jquery-upload-file-error']="File already exists";
    echo json_encode($custom_error);
    die();
*/
    $error = $_FILES["input"]["error"];
    //You need to handle  both cases
    //If Any browser does not support serializing of multiple files using FormData() 
    if(!is_array($_FILES["input"]["name"])) //single file
    {
        $fileName = $_FILES["input"]["name"];
        move_uploaded_file($_FILES["input"]["tmp_name"],$output_dir.$fileName);
        $ret[]= $fileName;
    }
    else  //Multiple files, file[]
    {
      $fileCount = count($_FILES["input"]["name"]);
      for($i=0; $i < $fileCount; $i++)
      {
        $fileName = $_FILES["input"]["name"][$i];
        move_uploaded_file($_FILES["input"]["tmp_name"][$i],$output_dir.$fileName);
        $ret[]= $fileName;
      }

    }
    echo json_encode($ret);

} else { echo "No data received."; }
 ?>
4

0 回答 0