0

I want to upload whole folder to server. I have found after extensive research to use webkitdirectory directory in input tag. its workinf fine and now it is also selecting folder from the dialog. but it is only showing list of all files in the folder it is not uploading full folder to the server.

My html code is as follows:

 <div class="upload" style="width: 30px; height: 30px; margin-top: -29px; margin-left: 90%; background: url(http://192.168.1.30/sannan/upload.png);">
                <input type="file" name="myfile" id="myfile" onchange="submitForm()" webkitdirectory directory/>

            </div>

My upload script is as follows:

<?php    
    $output_dir = "upload/"; 
  if(isset($_FILES["myfile"]))
    {
        //Filter the file types , if you want.
        if ($_FILES["myfile"]["error"] > 0)
        {
            echo "Error: " . $_FILES["file"]["error"] . "<br>";
        }
        else
        {
           foreach($_FILES["myfile"] as $file)
        {
            move_uploaded_file($file["tmp_name"],$output_dir. $_FILES["myfile"]["name"]); 
        }
            echo "Uploaded File :".$_FILES["myfile"]["name"];
        }

    }
?>

its only uploading first file in the folder to the server. I want to upload full folder including all files and subfolder to the server.

Muneem Habib
  • 1,046
  • 4
  • 18
  • 49
  • i doubt this is possible without using ajax or something – ddoor Sep 27 '13 at 07:11
  • i want an answer i can use everything i just want to upload folder to server – Muneem Habib Sep 27 '13 at 07:14
  • Why dont you select all using ctrl-A and upload ? – Voonic Sep 27 '13 at 07:18
  • Please double-check with this demo-video: [Folder Upload with webkitdirectory (Nov 2011; by Alan Layt; Vimeo)](http://vimeo.com/32464969) – hakre Sep 27 '13 at 07:18
  • @shadow its requuirement i want folder to be uploaded – Muneem Habib Sep 27 '13 at 07:20
  • @Muneem Habib: That normally does not work. Also you need to do `var_dump($_FILES)` if you want to learn more what is actually uploaded and if it's compatible with PHP. – hakre Sep 27 '13 at 07:20
  • @hakre is there any source code for that he didnt posted his code? – Muneem Habib Sep 27 '13 at 07:21
  • @MuneemHabib check this out buddy http://www.w3bees.com/2013/03/directory-upload-using-html-5-and-php.html – Voonic Sep 27 '13 at 07:22
  • You have a problem to deal with the `$_FILES` array. So for a good question you should also show how it looks like because otherwise others need to create a test-case only to reproduce. with a `var_dump()` of `$_FILES` it might be already clear. So just add it if you want to get some help. As you need to help others to help you. That's all. – hakre Sep 27 '13 at 07:25
  • @hakre its source code is not running – Muneem Habib Sep 27 '13 at 07:35
  • Well then you probably should read this: [How to get useful error messages in PHP?](http://stackoverflow.com/q/845021/367456) and check the [Reference - What does this error mean in PHP?](http://stackoverflow.com/q/12769982/367456) in case you have a PHP error there. Check with error logging because you're doing file-uploads, this requires configuring your php.ini file. – hakre Sep 27 '13 at 07:55
  • @hakre now its working but it is only uploading 6files and it is also not uploading subfolders its just uploading files? – Muneem Habib Sep 27 '13 at 07:56
  • @MuneemHabib: Please contact the vendor of the software you make use of a feature for your support and documentation options of that feature. The vendor is the first-hand instance who can tell you. This is especially necessary as you're using a non-standard browser feature, so things might be pretty much a moving target still. – hakre Sep 27 '13 at 07:57
  • @hakre thanks for replying me back. just tell me 1 thing can we upload folder in the server in the same structure as it is placed locally. Like if a folder has a sub folder then this subfolder will also be uploaded on server? – Muneem Habib Sep 27 '13 at 08:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38151/discussion-between-hakre-and-muneem-habib) – hakre Sep 27 '13 at 08:04
  • @Muneem Habib: I can not tell you. It's a new feature to me, I've never used it. However, if you get together the reference documentation and the var_dump and if you make yourself a little comfortable with PHP file uploads (it's in the manual) you should be able to solve this. Also we've got some good resources on Stackoverflow for multiple file uploads. I'd say you need to deal with the files your own in your PHP code however that should be no problem. This could be a standard multiple file upload. You can't upload directories, only files in HTML (conceptually not what the user thinks). – hakre Sep 27 '13 at 08:06
  • It could simply be PHP’s standard behavior of overwriting parameters with the same name, so that in the end you only get the last one. Try naming your upload file `myfile[]` instead, and then make a var_dump of `$_FILES` to set what you get. – CBroe Sep 27 '13 at 08:28
  • @CBroe i am getting 6 files but i have 54 files in my folder? – Muneem Habib Sep 27 '13 at 10:07

1 Answers1

0
<?php    
    $output_dir = "upload/"; 
  if(isset($_FILES["myfile"]))
    {
        //Filter the file types , if you want.
        if ($_FILES["myfile"]["error"] > 0)
        {
            echo "Error: " . $_FILES["file"]["error"] . "<br>";
        }
        else
        {
           foreach($_FILES["myfile"] as $file)
        {
            move_uploaded_file($file["tmp_name"],$output_dir. $file["name"]); 
        }
            echo "Uploaded File :".$_FILES["myfile"]["name"];
        }

    }
?>
Mayank Sharma
  • 844
  • 7
  • 21