Home > Backend Development > PHP Tutorial > How to Implement Multiple File Uploads in PHP using jQuery?

How to Implement Multiple File Uploads in PHP using jQuery?

Patricia Arquette
Release: 2024-11-01 06:04:02
Original
906 people have browsed it

How to Implement Multiple File Uploads in PHP using jQuery?

Multiple File Upload with PHP and jQuery

In this scenario, you're attempting to upload multiple files using PHP and jQuery. To accomplish this, follow the steps outlined below.

Index.html

<code class="html"><html>
    <head>
        <title>Load files</title>
        <script src="jquery.min.js"></script>
        <script>
            $(document).ready(function() {
                $("#myfiles").on("change", function() {
                    var myfiles = document.getElementById("myfiles");
                    var files = myfiles.files;
                    var data = new FormData();

                    for (i = 0; i < files.length; i++) {
                        data.append('file' + i, files[i]);
                    }

                    $.ajax({
                        url: 'load.php',
                        type: 'POST',
                        contentType: false,
                        data: data,
                        processData: false,
                        cache: false
                    }).done(function(msg) {
                        $("#loadedfiles").append(msg);
                    });
                });
            });
        </script>
    </head>
    <body>
        <div id="upload">
            <div class="fileContainer">
                <input id="myfiles" type="file" name="myfiles[]" multiple="multiple" />
            </div>
        </div>
        <div id="loadedfiles">
        </div>
    </body>
</html></code>
Copy after login

load.php

<code class="php">$path="myfiles/";//server path
foreach ($_FILES as $key) {
    if($key['error'] == UPLOAD_ERR_OK ){
        $name = $key['name'];
        $temp = $key['tmp_name'];
        $size= ($key['size'] / 1000)."Kb";
        move_uploaded_file($temp, $path . $name);
        echo "
            <div>
                <h12><strong>File Name: $name</strong></h2><br />
                <h12><strong>Size: $size</strong></h2><br />
                <hr>
            </div>
            ";
    }else{
        echo $key['error'];
    }
}</code>
Copy after login

The above is the detailed content of How to Implement Multiple File Uploads in PHP using jQuery?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template