如何上载文件夹的所有内容,包括文件夹和文件夹中的文件?

时间:2019-07-17 04:13:50

标签: php jquery html directory uploading

如何上传文件夹的内容,包括文件和文件夹内的文件夹?

我已经尝试过此功能,但是问题是 FILES 仅在上传,文件夹内的文件夹未在上传。

//html
<form method="post" id="user_form" enctype="multipart/form-data">
<div class="modal-content">
<div class="modal-header">
 <button type="button" class="close" data-dismiss="modal">&times;</button>
 <h4 class="modal-title">Add User</h4>
</div>
<div class="modal-body">
 <label>Enter Folder Name</label>
 <input type="text" name="text_firstname" id="text_firstname" class="form-control" />
 <br />
 <label>Select Folder</label>
 <input type="file" name="user_image[]" id="files" multiple directory="" webkitdirectory="" mozdirectory="" onchange="selectFolder(event)" />
 <span id="user_uploaded_image"></span>
</div>
<div class="modal-footer">
 <input type="hidden" name="user_id" id="user_id" />
 <input type="hidden" name="operation" id="operation" />
 <input type="submit" name="action" id="action" class="btn btn-success" value="Add" />
 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</form>

//script
<script type="text/javascript">
$(document).ready(function(){

 $(document).on('submit', '#user_form', function(event){
  event.preventDefault();
  var firstName = $('#text_firstname').val();
  if(firstName != '')
  {
   $.ajax({
    url:"insert.php",
    method:'POST',
    data:new FormData(this),
    contentType:false,
    processData:false,
    beforeSend:function(data)
    {
        $(".preload").show();
    },
    success:function(data)
    {
     // alert(data);
     $('#user_form')[0].reset();
     $(".preload").hide();
    }
   });
  }
  else
  {
   alert("Both Fields are Required");
  }
 });



});

function selectFolder(e) {
    var theFiles = e.target.files;
    var relativePath = theFiles[0].webkitRelativePath;
    var folder = relativePath.split("/");
    $("#text_firstname").val(folder[0]);
}

</script>

//insert.php ~ PHP CODE
<?php 

    if ($_POST['text_firstname'] != "") {
        $folder_name = "uploads" .'/'. $_POST['text_firstname'];
        if (!is_dir($folder_name))
            mkdir($folder_name);
        foreach ($_FILES['user_image']['name'] as $i => $name) {
            if (strlen($_FILES['user_image']['name'][$i]) > 1) {
                move_uploaded_file($_FILES['user_image']['tmp_name'][$i], $folder_name.'/'.$name);
            }
        }

        echo "FOLDER UPLOADED";
    }
    else
    {
        echo "ERROR";
    }

 ?>

我希望我上传的所有文件和文件夹都必须上传,包括文件夹和文件。请帮我解决这个问题。预先谢谢你!

0 个答案:

没有答案