How can we upload multiple files in php and store in database?

File upload in PHP is the most used functionality for the web application. A single file or multiple files can be easily uploaded using PHP. PHP provides a quick and simple way to implement server-side file upload functionality. Generally, in the web application, the file is uploaded to the server and the file name is stored in the database. Later the files are retrieved from the server based on the file name stored in the database.

In most cases, a single image is uploaded at once. But sometimes you have a requirement to upload multiple images at once. In this tutorial, we will show you how to upload multiple images in PHP and store the images in the MySQL database. Multiple image upload allows the user to select multiple files at once and upload all files to the server in a single click.

Our example code will implement the following functionality to demonstrate the multiple images upload in PHP.

  • HTML form to select multiple images.
  • Upload images to the server using PHP.
  • Store file names in the database using PHP and MySQL.
  • Retrieve images from the database and display on the web page.

Create Database Table

A table needs to be created in the database to store the image file names. The following SQL creates an images table with some basic fields in the MySQL database.

CREATE TABLE `images` [
 `id` int[11] NOT NULL AUTO_INCREMENT,
 `file_name` varchar[255] COLLATE utf8_unicode_ci NOT NULL,
 `uploaded_on` datetime NOT NULL,
 `status` enum['1','0'] COLLATE utf8_unicode_ci NOT NULL DEFAULT '1',
 PRIMARY KEY [`id`]
] ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Database Configuration [dbConfig.php]

The dbConfig.php file is used to connect and select the MySQL database. Specify the database hostname [$dbHost], username [$dbUsername], password [$dbPassword], and name [$dbName] as per your MySQL credentials.

File Upload Form HTML

Create an HTML form with an input field to allow the user to select images they want to upload.
The tag must contain the following attributes.

  • method="post"
  • enctype="multipart/form-data"

The tag must contain type="file" and multiple attributes.

    Select Image Files to Upload:
    
    

Upload Multiple Files in PHP [upload.php]

The upload.php file handles the multiple image upload functionality and shows the upload status to the user.

  • Include the database configuration file to connect and select the MySQL database.
  • Get the file extension using pathinfo[] function in PHP and check whether the user selects only the image files.
  • Upload images to the server using move_uploaded_file[] function in PHP.
  • Insert image file names in the database using PHP and MySQL.
  • Show the upload status to the user.

Upload Multiple Files and Images in CodeIgniter

Display Images from Database

Now we will retrieve the file names from the database and display the respective images from the server on the web page.

  • Include the database configuration file to connect and select the MySQL database.
  • Fetch images from MySQL database using PHP.
  • Retrieve images from the server [uploads/] and listed on the web page.

    

Chủ Đề