HTML5 Directory Upload Using PHP

HTML 5 allows you to upload a complete directory by using directory attribute. This method is more user friendly then any other file uploading method. Because we can upload a complete directory at a time. Till now this method is working well only in Chrome and webkit browser by using webkitdirectory attribute we can also add mozdirectory attribute for mozila browser support. Here is a simple example to upload directory using PHP.





HTML FORM
<form method="post" enctype="multipart/form-data">
    <input type="file" name="files[]" id="files" multiple="" directory="" webkitdirectory="" mozdirectory="">
    <input class="button" type="submit" value="Upload" />
</form>

PHP
$count = 0;
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    foreach ($_FILES['files']['name'] as $i => $name) {
        if (strlen($_FILES['files']['name'][$i]) > 1) {
            if (move_uploaded_file($_FILES['files']['tmp_name'][$i], 'upload/'.$name)) {
                $count++;
            }
        }
    }
}

No comments:

Post a Comment