VOOZH about

URL: https://www.geeksforgeeks.org/php/how-to-upload-images-in-mysql-using-php-pdo/

โ‡ฑ How to upload images in MySQL using PHP PDO ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to upload images in MySQL using PHP PDO ?

Last Updated : 23 Jul, 2025

In this article, we will upload images to the MySQL database using PHP PDO and display them on the webpage. 

Approach: Make sure you have a XAMPP server or WAMP server installed on your machine. In this tutorial, we will be using the WAMP server.

Article content:

  1. Table Structure
  2. Database configuration using PDO.
  3. HTML & PHP Files
  4. Working Procedure
  5. Conclusion

To upload images to the MySQL database using PDO-PHP and display them on the webpage, follow the steps given below:

1. Create Database: Create a database using PHPMyAdmin, the database is named โ€œgeeksforgeeksโ€ here. You can give any name to your database. You can also use your existing database or create a new one.

๐Ÿ‘ Image
create database โ€œgeeksforgeeksโ€

2. Create Table: We now create a table named โ€˜imagesโ€˜. The table contains three fields:

  • id โ€“ int(11) โ€“ primary key โ€“ auto increment
  • name โ€“ varchar(100)
  • image โ€“ varchar(255)

Your table structure should look like this:

๐Ÿ‘ Image
create table โ€œimagesโ€

Or copy and paste the following code into the SQL panel of your PHPMyAdmin.

CREATE TABLE `images`(
 `id` int(11) NOT NULL AUTO_INCREMENT, 
 `name` varchar(100) NOT NULL,
 `image` varchar(255) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

To do this from the SQL panel refer to the following screenshot:

๐Ÿ‘ Image
create a table from SQL panel

3. Creating folders and files:

We will now create a folder named โ€œuploadsโ€œ. The files uploaded by the client on the server will be stored in this folder. Create index.php, database_connection.php and view.php files. Keep your main project folder (for example.. GeeksforGeeks) in the โ€œC://xampp/htdocs/โ€, if you are using XAMPP or โ€œC://wamp64/www/โ€ folder if you are using the WAMP server respectively. The folder structure should look like this:

๐Ÿ‘ Image
folder structure

Database configuration using PHP PDO:

  • database_connection.php: Create a "database_connection.php" file for the database connection.

index.php: Below is the PHP source code for uploading the images using HTML form. Let us first understand the PHP part.

In the below code, the first block verifies that the โ€˜submitโ€™ button from the form has been clicked using the PHP isset() function. And the second if block verifies that the image file exists with a valid extension. $_FILES is a two-dimensional superglobal associative array of items that are being uploaded via the HTTP POST method. The move_uploaded_file() function is used to upload the pdf file to the server. We are passing 2 values, the temporary file name and the folder where the file will be stored. The files will be stored in the โ€œGeeksForGeeks/uploads/ โ€ folder which we created earlier.

In the HTML <form> tag, we are using โ€œenctype=โ€™multipart/form-dataโ€ which is an encoding type that allows files to be sent through a POST method. Without this encoding, the files cannot be sent through the POST method. We must use this enctype if you want to allow users to upload a file through a form. The multiple attributes in the input file element are used to enable selecting multiple files.

view.php: Code to display the uploaded images.

4. Working Procedure:

1. Run the local server or any server and redirect to your index.php page.

๐Ÿ‘ Image
 

2. Select your image file to upload and click on submit button to upload the image to the database.

๐Ÿ‘ Image
 

3. Click on the view upload link to check the uploaded file.

๐Ÿ‘ Image
 

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.

๐Ÿ‘ Image
 

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

Comment
Article Tags: