article

Tuesday, May 23, 2023

PHP Uploading multiple files

PHP Uploading multiple files

Bootstrap 5
https://getbootstrap.com/docs/5.0/getting-started/introduction/

index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//index.php
<?php require("script.php"); ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PHP Uploading multiple files</title>
</head>
<body>
<div class="container">
    <h2>PHP Uploading multiple files</h2>
    <form action="" method="post" enctype="multipart/form-data">
    <div>
      <label for="formFileLg" class="form-label">Select the files you want to upload</label>
      <input class="form-control form-control-lg" id="formFileLg" type="file" name="files[]" multiple>
    </div>
    <br/>
    <button type="submit" class="btn btn-primary" name="upload">Upload files</button>
    </form>
</div>
</body>
</html>
script.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//script.php
<?php
    if(isset($_FILES['files'])){
        $folder = "uploads/";
        $names = $_FILES['files']['name'];
 
        $tmp_names = $_FILES['files']['tmp_name'];
        $upload_data = array_combine($tmp_names, $names);
         
        highlight_string("<?php " . var_export($upload_data, true) . ";?>");
 
        foreach ($upload_data as $temp_folder => $file) {
            move_uploaded_file($temp_folder, $folder.$file);
        }
 
    }  

Related Post