Uploading Multiple Images Into Database Using Php
How to upload multiple files and shop in the MySQL database using PHP
In this article, you lot will acquire how to upload multiple files and store them in the MySQL database using PHP. It is very like shooting fish in a barrel to upload a single file and store it in the database, only sometimes there is too a need to upload and store multiple files, like uploading multiple images, PDF, docs and then on. Multiple files upload allows the user to choose multiple files at once and upload all files to the server. Uploading multiple files is the virtually used functionality for the web application.
Commencement, permit's create an HTML course to upload multiple files and store them in the MySQL database. The enctype='multipart/form-information' course attributes allow files to exist sent through post.
Information technology is required to write 'multiple' in file input to select and upload multiple files. Here, we have taken a file array in the input name to send multiple files in the mail service.
<form method='post' action='#' enctype='multipart/class-information'> <div class="form-group"> <input type="file" name="file[]" multiple> </div> <div class="form-group"> <input blazon='submit' name='submit' value='Upload' class="btn btn-primary"> </div> </form> Adjacent, create a database to shop files. Yous can either copy paste this CREATE statement in your database or apply your existing one.
CREATE Table `files` ( `id` int(eleven) Non NULL AUTO_INCREMENT, `file_name` varchar(255) COLLATE utf8_unicode_ci Non NULL, `uploaded_on` datetime NOT NULL, `status` enum('ane','0') COLLATE utf8_unicode_ci NOT Nada DEFAULT '1', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; Side by side, we have written the database connection code. Make sure to supplant 'hostname', 'username', 'password' and 'database' with your database credentials and proper name.
$conn = mysqli_connect('hostname', 'username', 'countersign', 'database'); //Bank check for connection mistake if($conn->connect_error){ dice("Error in DB connection: ".$conn->connect_errno." : ".$conn->connect_error); } Adjacent, we have written code to bank check submitted files, save files in the local directory and shop the uploaded file path in the database. The move_uploaded_file() function of PHP uploads images to the server.
if(isset($_POST['submit'])){ // Count total uploaded files $totalfiles = count($_FILES['file']['name']); // Looping over all files for($i=0;$i<$totalfiles;$i++){ $filename = $_FILES['file']['name'][$i]; // Upload files and store in database if(move_uploaded_file($_FILES["file"]["tmp_name"][$i],'upload/'.$filename)){ // Paradigm db insert sql $insert = "INSERT into files(file_name,uploaded_on,status) values('$filename',now(),1)"; if(mysqli_query($conn, $insert)){ echo 'Information inserted successfully'; } else{ echo 'Fault: '.mysqli_error($conn); } }else{ echo 'Error in uploading file - '.$_FILES['file']['proper noun'][$i].'<br/>'; } } } Consummate Script
Here, we accept merged the above code to upload multiple files to the database.
<?php //Database Connection $conn = mysqli_connect('hostname', 'username', 'countersign', 'database'); //Check for connectedness error if($conn->connect_error){ die("Error in DB connection: ".$conn->connect_errno." : ".$conn->connect_error); } if(isset($_POST['submit'])){ // Count full uploaded files $totalfiles = count($_FILES['file']['name']); // Looping over all files for($i=0;$i<$totalfiles;$i++){ $filename = $_FILES['file']['proper noun'][$i]; // Upload files and store in database if(move_uploaded_file($_FILES["file"]["tmp_name"][$i],'upload/'.$filename)){ // Image db insert sql $insert = "INSERT into files(file_name,uploaded_on,status) values('$filename',now(),i)"; if(mysqli_query($conn, $insert)){ echo 'Data inserted successfully'; } else{ echo 'Error: '.mysqli_error($conn); } }else{ echo 'Error in uploading file - '.$_FILES['file']['name'][$i].'<br/>'; } } } ?> <html> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/four.3.1/css/bootstrap.min.css"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/iv.3.1/js/bootstrap.min.js"></script> </head> <trunk> <div class="container"> <h1>Select Files to Upload</h1> <form method='mail service' action='#' enctype='multipart/form-information'> <div class="form-group"> <input type="file" name="file[]" id="file" multiple> </div> <div grade="course-group"> <input type='submit' name='submit' value='Upload' class="btn btn-primary"> </div> </course> </div> </body> </html>
Related Articles
PHP sanitize input for MySQL
PHP random quote generator
PHP String Contains
PHP calculate percentage of total
PHP Fix: invalid statement supplied for foreach
Locking files with flock()
PHP Brandish PDF file from Database
How to read CSV file in PHP and shop in MySQL
Generating discussion documents with PHP
PHP SplFileObject Examples
How to Upload a File in PHP
Simple PHP email class
Password reset system in PHP
HTTP authentication with PHP
PHP file cache library
PHP get current directory url
How to preclude CSRF attack in PHP
Forgot Password Script PHP mysqli database
PHP Contact Form with Google reCAPTCHA
HTML Grade Validation in PHP
Insert in database without page refresh PHP
brannonvirinarlecou.blogspot.com
Source: https://www.etutorialspoint.com/index.php/203-how-to-upload-multiple-files-and-store-in-mysql-database-using-php
0 Response to "Uploading Multiple Images Into Database Using Php"
Post a Comment