Introduction:

Today we shall learn how to upload multiple files through PHP multi-file upload code. In my previous lecture”PHP file upload become easier now“, I told you how to upload single file through PHP but today we shall learn about “PHP multi-file upload”. First you have to create Database and a table in the database. The new users who don’t know how to do it, just visit my previous lecture”PHP file upload become easier now“. The method will be same for  “PHP multi-file upload” as we did in single file upload. We need a HTML file first in which we shall create form which is to be displayed on the window for “PHP multi-file upload”. After creating this HTML page we shall make PHP page. This PHP page contains our whole working on HTML, how to connect to the database, how to pick file and save it to the database. The whole “PHP multi-file upload” code contain these things. In my  “PHP multi-file upload” code I shall use If statements to check my query, weather is it working or not? The “PHP multi-file upload” code will be provided to you below with the images for help.

Points to be remembered:

1) Create database.

2) Create table.

3) Make HTML file.

4) Make PHP file.

5) View the database.

Step 1) HTML file:

First you will take a HTML file type and write this code for PHP multi-file upload. The code is provided below for your ease….

Description:

In any “IDE” you like to use, write this HTML code. You can see I created a form in the body of the HTML for “PHP multi-file upload”. This form will upload the file.

I gave some properties to the form.

1) The action property is used to go to the next page after the button is pressed.

2) The method property is used to tell the type of form. We wanted to upload files and send them to the data base we use it. We will take it as “post“.

3) The last property is enctype=”multipart/form-data”. This is the property used to make the form work.

In the form I took two “input” tags.

1) First is for choosing the files from the browser.

2) The second one is button for “PHP multi-file upload”.

The most important thing, just look at the “first input tag”. I gave the type “file” which defines the file type you wanted to select or upload from the browser. Then I gave it a name which I shall be using for my “PHP multi-file upload” code and in the last I  used the property “multiple“. The multiple property is a property that takes “multiple files” to upload at a time.

The image is provided for your help.

PHP multi-file upload

Step 2) PHP file:

For the PHP code we shall take a file type PHP, in it our whole coding will be implemented.

1) First create the database connection as shown below.

Description:

For creating the connection we need few variables.

1) $servername = ‘localhost’ . To save the server name as defined by the name. You can give the name whatever you like to.

2) $username = ‘root’. This contains my user name, by default it is root so I gave it.

3) $password = ”. As you can see I kept it empty because during installation I didn’t gave password. If you have given then you have to write it down.

4) $database = ‘first_lecture’. The last variable contain my database name. You have to replace it by the name of you database.

5) “$dblink” this variable I used to save my connection query. It contains three variables. Keep in mind always place the variables in the sequence.

6) The last variable is used to check the connection. Weather is it linked or not…

Step 3) The if statement:

These are the “if” statements I used in my PHP multi-file upload code to check my connection.

Description:

Just look clearly in first “if” statement I used the name “upload_file” that I gave to “first input tag” of HTML. This statement will check weather the files are uploaded or not?

The second “if” statement will tells weather the files are send to the database or not?

The third “if” statement is used to check the connection of “PHP multi-file upload” code.

Step 4) The insert query:

The function and the INSERT PHP multi-file upload query in shown below.

Description:

The variable $name contains the upload function. It contains two arrays the first array contains the name on the “input tag” and the second array save the name key of it. The variable “$_FILES” is a “predefined variable“, it’s function is to upload files.

Now we shall use the SQL query  to insert files in “PHP multi-file upload table”. My table name is “multi_files” I gave it as per the topic, you can give what ever you like to. Then I wrote my column name and in the value I stored the variable which contains the file.

In the end for executing the query, I saved it in a variable “result“.

Step 5) Checking the INSERT query:

This if statement I used to check weather the files are uploaded or not? If they are successfully uploaded it will print first message else it will print the second one on failure.

Description:

This “if else” statements I used to check “PHP multi-file upload” code. weather the files are properly uploaded or not. After we click the upload button, If there is no error “then PHP multi-file upload” code will return a message “Success! Your files was successfully uploaded“. On the other hand if there comes an error in “PHP multi-file upload” then this message will appear “Error! Failed to insert files“.

If the file is uploaded successfully this view will come on the screen.

PHP multi-file upload

If the file is not uploaded successfully then this message will appear.

In the end I printed a link to go back on the form page. The link is shown below.

Step 6) The whole PHP multi-file upload code view:

The whole PHP code of “PHP multi-file upload” will be like this, as shown below.

Step 7) Some Images description:

The below window will appear when we shall click on “Choose Files” option.

PHP multi-file upload

Description:

In the above image you can see there is written “Choose Files” Which tells us that we can choose multiple files at a time. On clicking it the new window will appear through which you can select the files and then click open. The next step is shown below.

PHP multi-file upload

You can see there are multiple items are selected at a time. It tells that you can upload multiple files through “PHP multi-file upload” code.

PHP multi-file upload

So after clicking open the above window will appear. I selected eight files that’s why it is showing the number of file are “8“. Now just click the “Upload file” button to upload them.

The database will show you the names of the files in “files” column. And will generate “ID” to them.

PHP multi-file upload

Precautions:

Make shore that while using this code do replace the names of  “Database” , “Table” and “Column” with yours. The one who doesn’t know how to create Database and Table, kindly visit my previous lecture “PHP file upload become easier now“, it will help you in this matter.

Conclusion:

I think it is the easy way for uploading multiple files through “PHP multi-file upload” code. I made it easier for you. It is helpful for everyone even the beginners can get help through it.  You have seen I made the HTML page first. In which I created a form, which is to be displayed. Then the PHP page which contains the two queries and the if statements to check the queries. The database connection is created.

  • saran raj

    i can’t able to insert multiple images ?? for loop is missing or something ??