Reputation: 691
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?
Upvotes: 17
Views: 51396
Reputation: 1700
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!!";
}
?>
Upvotes: 2
Reputation: 529
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.
Upvotes: 33
Reputation: 424
<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.
Upvotes: 3
Reputation: 912
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/
Upvotes: 2
Reputation: 900
Give this PHP script a go:
Main website: http://www.uploadify.com/
Documentation: http://www.uploadify.com/documentation/
Upvotes: 0
Reputation: 1141
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
Upvotes: 1