How to upload images in MySQL using PHP PDO ?
In this article, we will upload images to the MySQL database using PDO-PHP and display it on the webpage.
Approach: It can be done by creating a MySQL table with image as a column of datatype varchar.
Create a folder in your project in which your actual image will be stored.
Note: Create an upload folder for storing the image files in your project folder.
Article content:
- Table Structure
- Database configuration using PDO.
- HTML & PHP Files
- Working Procedure
- Conclusion
1. Table Structure: The following are the MySQL queries to create a database and create a table where the database name is “gfg” and table name is “images”.
CREATE DATABASE gfg;
CREATE TABLE images (`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, `name` varchar(80) NOT NULL, `image` varchar(80) NOT NULL) ;
2. Database configuration using PHP PDO:
PHP code: Create a PHP “database_connection.php” file for the database connection.
PHP
<?php $server = "localhost" ; $username = "root" ; $password = "" ; $dbname = "gfg" ; try { $conn = new PDO( "mysql:host=$server;dbname=$dbname" , "$username" , "$password" ); $conn ->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); } catch (PDOException $e ) { die ( 'Unable to connect with the database' ); } ?> |
3. HTML and PHP files:
index.php: In the following example, multiple attributes in the file element are used to enable selecting multiple files.
PHP
<?php include "database_connection.php" ; if (isset( $_POST [ 'submit' ])) { // Count total files $countfiles = count ( $_FILES [ 'files' ][ 'name' ]); // Prepared statement $query = "INSERT INTO images (name,image) VALUES(?,?)" ; $statement = $conn ->prepare( $query ); // Loop all files for ( $i = 0; $i < $countfiles ; $i ++) { // File name $filename = $_FILES [ 'files' ][ 'name' ][ $i ]; // Location $target_file = 'upload/' . $filename ; // file extension $file_extension = pathinfo ( $target_file , PATHINFO_EXTENSION); $file_extension = strtolower ( $file_extension ); // Valid image extension $valid_extension = array ( "png" , "jpeg" , "jpg" ); if (in_array( $file_extension , $valid_extension )) { // Upload file if (move_uploaded_file( $_FILES [ 'files' ][ 'tmp_name' ][ $i ], $target_file ) ) { // Execute query $statement ->execute( array ( $filename , $target_file )); } } } echo "File upload successfully" ; } ?> <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > <title>Geeks for geeks Image Upload</title> </head> <body> <h1>Upload Images</h1> <form method= 'post' action= '' enctype= 'multipart/form-data' > <input type= 'file' name= 'files[]' multiple /> <input type= 'submit' value= 'Submit' name= 'submit' /> </form> <a href= "view.php" >|View Uploads|</a> </body> </html> |
view.php: The following file is the code content for the “view.php” file used in the above code.
PHP
<?php include "database_connection.php" ; ?> <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > </head> <body> <?php $stmt = $conn ->prepare( 'select * from images' ); $stmt ->execute(); $imagelist = $stmt ->fetchAll(); foreach ( $imagelist as $image ) { ?> <img src= "<?=$image['image']?>" title= "<?=$image['name'] ?>" width= '200' height= '200' > <?php } ?> </body> </html> |
4. Working Procedure:
1. Run local server or any server and redirect to your index.php page.
2. Choose a file to upload.
3. Click on submit button to upload the image to the database.
4. Click on the view upload link to check the uploaded file.
5. Conclusion: You can add CSS and change HTML as per the application requirement. In the above, the working of this upload image feature in PHP is implemented.