PHP Uploading Multiple Files on a single page.
index.php
Put this code in 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 25 26 27 28 29 30 31 |
<!DOCTYPE html> <html> <head> <title>PHP Uploading Multiple Files on a single page </title> </head> <body> <div style="height:40px;"></div> <div style="margin:auto; padding:auto; width:90%;"> <span style="font-size:25px; color:red"><center><strong>Uploading Multiple Files into MySQL Database on a single page using PHP/MySQLi</strong></center></span> <form method="POST" action="upload.php" enctype="multipart/form-data"> <input type="file" name="upload[]" multiple> <input type="submit" value="Upload"> </form> </div> <div width:80%;"> <h2>Output:</h2> <?php include('conn.php'); $query=mysqli_query($conn,"select * from photo"); while($row=mysqli_fetch_array($query)){ ?> <img src="<?php echo $row['location']; ?>" height="150px;" width="150px;"> <?php } ?> </div> </body> </html> |
conn.php
Put this code in conn.php.
1 2 3 4 5 6 7 8 9 |
<?php //MySQLi Procedural $conn = mysqli_connect("localhost","root","","upload"); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } ?> |
upload.php
Put this code in upload.php.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php include('conn.php'); foreach ($_FILES['upload']['name'] as $key => $name){ $newFilename = time() . "_" . $name; move_uploaded_file($_FILES['upload']['tmp_name'][$key], 'upload/' . $newFilename); $location = 'upload/' . $newFilename; mysqli_query($conn,"insert into photo (location) values ('$location')"); } header('location:index.php'); ?> |
Download Code
PHP Uploading Multiple Files on a single page