In this file our main focus is to learn, how to write PHP file upload code and makes it in working order? I shall make it too simple so that it should help the beginners to work on.Remember this, for making this project we need an HTML code with PHP file upload code. First of all we have to make a database and table in the database. After submitting the file it has to be saved somewhere in the database. First of all we will make a “Database” for our PHP file upload project. After making it we will create a “table” in our database for storing the data. We only need two columns, one for the “id” and one to store information. give both of them length, the detail I shall explain on wards. After this we shall make our HTML file, just create a simple form in it. After doing this our focus will be on PHP file upload code. We shall make connection to the database, use “if” statement to check our database(weather is connection working or not?). Then we will use INSERT QUERY to insert data in the “table” and an “if” statement to check(weather is the query working or not?). I shell provide the images of every step for your ease.

These are the points to be remembered:

1) Create Database.

2) Create Table.

3) Do HTML Coding.

4) Do PHP Coding.

5) Check the Database.

STEP 1) Create Database:

Type “localhost/myphpadmin/” in the link bar. It will open the window for you. Now in the left bar click
on new and name the database what ever you like to. I name it as “first_lecture“. Then click on “Create” your database is created. The image is provided bellow for your ease.

PHP file upload

 As you can see in the above picture, I named the database as “first_lecture“. then just click the “Create” button. Then the new window will appear shown below:

PHP file upload

You can see the database name on the left hand of the page. So the database is created….

 STEP 2) Create Table:

You can see after creating the database the new window appears, there is written create table. Now give
it a name as I shall name my table as related to the topic “files_upload“, you can take whatever you like to. I shall take two fields for this. These fields are as follow. Give them type and length. The “id” should be “primary key“.
The image is provided bellow for your ease.

PHP file upload

This is the window of creating table. It comes after clicking on our database. I named my table as “file_upload“. You can name it what ever you like to. I made two columns as per need. Then click “Go“. Then the new window will appear which is as fallow…

PHP file upload

In this window we will set our columns properties. First give them names, the first should be “id” and we set it as “Primary Key” with “Auto Increment“. Why we did so? Because the id remains fixed and it will be assigned by the database itself. Give them length and type and then click “Save“.

After this a new window will appear, shown below:

PHP file upload

As you can see on the left side, down our database name our table appeared.


Now we will first write our HTML code in any “IDE” you like to.

HTML code brief description:

The above HTML code contains a simple form inside the body. We will take the “method” as “past” because we wanted to upload the form and wanted to send the data into the database. I used to input tags for my file. The first tag will give you the option to choose a file from the browser you wanted to upload. As you see I gave the type “file” , it defines the file type you wanted to select or upload from the browser. Then I assigned the “name” to my tag. The name I assigned because I shall be using it in my PHP file upload code.

The second input is a button, as you can see from its type “submit“, when you will click this button after selecting the file from the browser, it will submit the file to the database. As you see I assigned “value” to my button, this will show the text written on the button. Then end the tags after all this.The form must have
encytype=multipart/form-data“, if not then it will not work.

The HTML file will appear like this in the browser. The image is shown below:

PHP file upload

Now click on choose, the browser will open from where you can select the file. The image it shown below….

PHP file upload

 Just select the file and click open.

STEP 4) PHP file:

This above code contains variables that makes our link in the server localhost of the database. The serername is “localhost“. The username is “root“. I remained the password field empty because I have not assigned password while installing my XMPP, if you have assigned it then you should give password and in the end the last variable contains the name of the “database” in which the table is. This is that database we made earlier.

STEP 5) If statement/database link:

This upper code contains an “if” statement, which I created to check weather the file is uploaded or not and it also contains my connection statement. This query will link me to the database and after that there is another “if” statement to check weather the “connection” succeeded or not. If the connection failed it will return a message of connection failed.

STEP 6) Upload function and INSERT query of the table:

Now I took a variable “$name“,it contains the name of that field which takes the file to be uploaded. As you remember I gave a name to the input field in HTML “upload_file” this name calls that field. The variable “$_FILES” is a “predefined variable“, it’s function is to upload files. The first string contains the file while the second string contains it’s name. Then I used the “INSERT” query, if you forgets the structure of the query just go to the
localhost, select your table and click on “SQL” then click on INSERT. The query will appear, copy that and place it in your code to get rid of the errors. In the end I stored the query in to a variable “$result“. Why I did so? Just to check weather the query works or not.

The image of our upload_file is given below:

PHP file upload

As you can see the file name is displayed with the file type. Now click the upload button.

The image of the “query” is given below. You can copy it from there….

PHP file upload

STEP 7) If statement to check the INSERT query:

The if statement I used here just to check weather the “INSERT query” worked or not. That’s why I placed my query in a variable. It states that if the query works it will echo a message “Success! Your file was successfully uploaded!” and if it fails to work then it will echoError! Failed to insert the file“. These are the two conditions to check the query. The image is shown below..PHP file uploadSo what is this? It is the message we stored in if statement, which tells that the file is uploaded. Now let’s view our database’s table “files_upload“, to check weather the data is saved or not?

PHP file upload

Now look at this image, see the data is saved in the table with “id = 1“. This is how it works.


STEP 8) Creating a link to go back:

This is the link I created to go back to the form page anytime you wanted to….

LAST STEP) The whole code of  “PHP file upload” will be like this:

This is PHP file upload detailed code, which you can easily run into your browser.

In my this whole PHP file upload code, I made a connection to the database and used INSERT query for PHP file upload code. I used to if statements to check my database connection and query .You can just copy the code from here and run it into your browser. The beginners sometimes confuses that which software will they use. You can use “Dreamweaver, Notepad or even file type text” I hope you will be satisfied by this PHP file upload lecture …


Keep in mind, the table name and the database name should be in small letters, even the HTML and PHP pages names. This rule is followed by all the good programers and make a habbit of placing semi-colon while doing PHP at the end of every statement.


So what we got through it. An easy way in PHP file upload. We went to the server and created a database and in the database created a table. After this we created a new page of HTML and made a simple form of two fields. One to picks a file and other was the button. Then we created a new PHP page, linked it to the server and database. Inserted our queries and used if statements to check weather our query worked or not. In the end made a link to take us back to our form. Now it is easy for everyone to use this PHP file upload code. I know it it was difficult for the programmer to make PHP file upload code earlier but I made it easier for everyone now.

It is used in most of the websites for uploading files. Mostly the it is complex for the developer, but now I made PHP file upload code easier to read and learn. When you will be using my this PHP file upload code, just make it clear that before you run PHP file upload code in the browser, replace the database and table names with yours and then run this PHP file upload code. Otherwise PHP file upload code will give you syntax error message.

All the information for PHP file upload code is provided to you in a single file. This PHP file upload code will give a great benefit to the developer even they are the beginners or master. PHP file upload code is essential tool. I made it simple for you to understand PHP file upload code. Hope you liked it……