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
//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>
	<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</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
//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