3

I am working on PHP and i am stuck where i want to get folder/directory name which i select in file upload control.

It is working fine, but i am not able to get folder name using jQuery.

 <input type="file" name="files[]" id="audios" multiple="" directory="" webkitdirectory="" mozdirectory="">

My code is like above, i just want to get folder name instead of all file names included in that folder.

Any help will be great, Thanks in advance :)

KMG
  • 114
  • 7
  • 1
    Possible duplicate of: https://stackoverflow.com/a/15201258/5503275 – DsRaj Jun 07 '18 at 06:08
  • Not a duplicate. The `webkitdirectory` attribute is meant to include relative paths of the uploaded files. They are visible in the Firefox request inspector but removed on the PHP side. I'll probably report a bug in PHP. I think it's a case of "we, PHP, take away all your sorrows (and features)". – ygoe Dec 29 '18 at 22:41

1 Answers1

0
 -> Written by Mangi Ganesh <-       
<html>
          <head>
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
          <script>
          
           $(document).ready(function(){
          document.getElementById("files").addEventListener("change", function(event) {
          var files = event.target.files;
        
            var item = document.createElement("li");
            var str = item.innerHTML = files[0].webkitRelativePath;
            
            item.innerHTML = str.substring(0, str.lastIndexOf("/") + 0);
            $("#foldername").val(item.innerHTML);
          
        }, false);
        });
        
          </script>
          </head>
          <body>
        
          <form action="#" method="post" enctype="multipart/form-data"> 
          <input type="text" name="foldername" id="foldername" /><br/><br/>
          Upload Folder: <input type="file" name="files[]" id="files" multiple directory="" webkitdirectory="" moxdirectory="" /><br/><br/> 
          <input type="Submit" value="Upload" name="upload" />
         
          </form>
          </body>
          </html>
    
      <?php
      if(isset($_POST['upload']))
      {
        if($_POST['foldername'] != "")
        {
            $foldername=$_POST['foldername'];
            if(!is_dir($foldername)) mkdir($foldername);
            foreach($_FILES['files']['name'] as $i => $name)
            {
                if(strlen($_FILES['files']['name'][$i]) > 1)
                {  move_uploaded_file($_FILES['files']['tmp_name'][$i],$foldername."/".$name);
                }
            }
            echo "Folder is successfully uploaded";
        }
        else
            echo "No Folder";
      }
      ?>
Ganesh
  • 1
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 29 '22 at 06:54