14

I'm developing a website where multiple images are uploaded to a website and I'm playing around with ways that this can be done. At the moment I have a php script that will get & display images from a given folder which looks like this:

<?php
$dirname = "content/2014/February/";
$images = glob($dirname."*.*");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
?>

This works fine and I can format the <img> using css and apply jquery for the gallery, BUT, how can I upload the folder of images using php and a html form in the first place?

user3177012
  • 663
  • 2
  • 8
  • 19
  • 1
    Possible duplicate of [How to upload folder with PHP?](http://stackoverflow.com/questions/4008406/how-to-upload-folder-with-php) – Jonathan Hall Jun 25 '16 at 16:12

6 Answers6

29

It is possible to upload a folder now. You can get it done by following below code:

  <input type="file" webkitdirectory mozdirectory />

You can check the demo here: https://jsfiddle.net/kevalpadia/vk6Ldzae/

I hope it will help you solve your issue.

Nimblechapps
  • 489
  • 5
  • 5
3

The Current Answer is NOT supported by all browsers.

You can not upload a whole folder.

currently only chrome supports it

And to upload many files http://www.uploadify.com/

Community
  • 1
  • 1
CS GO
  • 914
  • 6
  • 20
  • Thanks for the information. The problem with Uploadify is that it's for non-commercial use only and I find that using Chrome on my XP machine causes blue-screen. I will have to think of another way round this I think – user3177012 Feb 26 '14 at 12:48
  • @user3177012 a bluescreen is *always* an os problem (os, driver, ...). Chrome or any app can crash, your OS can give error messages, warnings, event logs or whatever, but crashing isn't an acceptable option and is an os or driver bug – ymajoros Dec 18 '15 at 13:03
  • Which one is the current answer?!? Nothing has been accepted. – Deanie Aug 11 '20 at 22:58
3

<input type="file" webkitdirectory="" directory="" /> - this works just on few/modern browsers- like Edge or Webkit engines (Chrome). I think that is not supported by Firefox.

Boris Delev
  • 424
  • 3
  • 16
  • 5
    it is now officially supported in firefox as well since v50.0 and MS edge too. just if anyone comes looking around. – Wdy Dev Aug 29 '17 at 10:42
1

You can use HTML5's

<input type="file" multiple>

About processing uploads in PHP, read more here: http://php.net/manual/en/features.file-upload.post-method.php

kazy
  • 1,111
  • 2
  • 14
  • 24
1

Yes, It is possible. Here is the code:

<form method="post" enctype="multipart/form-data" action="#">
        Folder Name: <input type="text" name="foldername" /><br/>
        Choose Directoryy:  <input type="file" name="files[]" id="files" multiple directory="" webkitdirectory="" mozdirectory=""><br/>
    <input class="button" type="submit" value="Upload" name="upload" />
</form>

<?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 uploaded successfully ..";
        }
        else
        echo "Folder uploaded Failed!!";
}
?>
Ratnesh
  • 1,554
  • 3
  • 12
  • 28
0

Give this PHP script a go:

Main website: http://www.uploadify.com/

Documentation: http://www.uploadify.com/documentation/

Vrutin Rathod
  • 900
  • 1
  • 12
  • 16