Home  • Programming • PHP

Multiple File Upload using HTML and PHP

Sometime we need to upload multiple file in the server.It is so boring to send one by one. It is very helpful if we send multiple file in one time. I describe a very easy way to send multiple file in one time. I guess every body know how to send one file in the server. If we send multiple file we need to add one additional attribute multiple in the input tag. I use another attribute directory . It is not necessary If you browser is updated Other wise it will very necessary to upload multiple file.Maximum time Internet Explorer make trouble.So it is safe to use directory attribute. The total code in bellow.
<?php
$count = 0;
if (isset($_POST['upload'])){
    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++;
            }
        }
    }
}
?>
<!DOCTYPE HTML>
<html>
    <head>
        <title>Directory Upload using webkidriectory</title>
        <style>
            *{
                margin: 0;
                padding: 0;
            }
            a{
                text-decoration: none;
                color: #333;
            }
            body {
                text-align:center;
                background-color:#61b3de;
                font-family:Arial, Helvetica, sans-serif;
                font-size:80%;
                color:#666;
            }
            .wrap {
                background: #f3f8fb;
                width:730px;
                margin:30px auto;
                border: 4px dashed #61b3de;
                border-radius:4px;
                padding: 20px 5px;
            }
            
            
            
            .msg{
                background: #ddeff8;
                padding: 5px;
                margin-bottom: 15px;
                border: #61b3de dotted 1px;
            }
            
        </style>
    </head>
    
    <body>
        <div class="wrap">
            <?php
            if ($count > 0) {
                echo "<p class='msg'>{$count} files uploaded</p>

";
            }
            ?>
            <h1>Multiple File Upload using HTML and PHP</h1>
            <form method="post" enctype="multipart/form-data">
                <input type="file" name="files[]" id="files" multiple="" directory="" webkitdirectory="" mozdirectory="">
                <input class="button" type="submit" name="upload" value="Upload" />
            </form>
           
        </div>
    </body>
</html>

If you want the user can upload only image file then you add accept attribute

<input type="file"  name="files[]" multiple="multiple" accept="image/*" />

Comments 0


Share

Copyright © 2024. Powered by Intellect Software Ltd