3 Simple Steps to Create File Upload Script in PHP & MySQL With Size, Extension Validation

3 Simple Steps to Create File Upload Script in PHP & MySQL With Size, Extension Validation

This is a simple File Upload Script in PHP & MySQL, uploading file into the server and also inserting uploaded file information into the database. And also we are writing validations for uploading file with size restrictions & file extension validation. Here I’m using Image format restriction, you can use any file type you want based on your need.

Then we will insert this uploaded file information into database, by using this data we will fetch the information and delete the files information from directory and also from database.

First of all, we need to create the form in HTML and some styles. I’m using bootstrap default styles and also some custom styles. You can get them from below.

1. Simple HTML Form to Upload File In PHP

This is a simple HTML form with file input field and a submit button.

Adding Bootstrap Default Styles

Add these style sheets in the head section of your HTML File to load bootstrap style.

Adding Custom Styles to Upload Form

Add these styles into a style sheet and save the file as styles.css.

Uploading File into Server

Before Uploading File into Server, we should know about $_FILES super global. By using this $_FILES super global we need to access some information from the uploaded file.

We should know the name of the uploaded file. And the size of the file, extension of the file that is being uploaded. And the temporary location of the file uploaded. Below is the information

We can read name of the file $_FILES[‘name-of-file-field’][‘name’]

We can get the size of the file $_FILES[‘name-of-the-field’][‘size’]. This file size is in bytes.

We can get the type of the file $_FIELS[‘name-of-the-field’][‘type’] . This is file type that is image, video or any other file type.

We can get the file’s temporary uploaded location by $_FILES[‘name-of-file-field’][‘tmp_name’]. We are going to use this information to move the uploaded file into our desired directory.

Till Now we have seen the basics, let’s get into the script.

Checking to see that if the file name is set and not empty. Then setting a directory variable, and moving file from temporary location to our desired location.

By using move_uploaded_file function, we are successfully uploading the file.

Next we need to add the validations or restrictions based on the file type and also based on the size.

Restrict File Upload size while Uploading in PHP

We can achieve this by simply writing an if else condition, we will use the file size from Files superglobal. Before doing this we should define a variable with maximum file size, here I’m using 100kb as my maximum file size. You can increase this to your need.

Add this condition inside the first if statement, below is the code.

Restrict File Upload based on Extension While Uploading

For restricting file uploads based on the extensions, it’s tricky but not that’s complicated. First of all, we need to read the file extension. For that We will write PHP code do this function, we will remove everything in front of the period and also the period in the file name. So, the remaining part is the is the file extension.

We can get the output by using two functions, one is strpos and the another one is substr.

Add the condition to check the file extension inside the filename checking condtion. We can use these conditions in combination with file size restriction condition.

Displaying Success and Failure Messages in PHP File Upload Script

I’ve already defined failed message variable as fmsg and sucess message variable as smsg, you can use this below code to print the messages. Add this code inside the form opening tag.

2. Inserting Uploaded File information into database for future access.

Till now we are able to upload the file into our web server, next we need to add this file information into the database. So that we can display all the files list from database. For viewing and deleting.

For this, we need to create a table with the following fields. Id for assigning a number to each record. File name , file size, file type and the location of the file on server.

You can create the table using below SQL code.

Create Database Table

After executing the move_uploaded_file function, in the success condition, we will insert a record into our database with file information.

For this We need to connect to the Datbase

Connect to Datbase

Create connect.php file with below code.

Add this code after move_uploaded_file function condition

Uploaded few files using this code, you can see these records in database.

Uploaded few files using this code, you can see these records in database.

Viewing Uploaded Files From PHP File

Create a File with the name of view.php, this file is for displaying all the uploaded files information. This information is retrieved from the database.

Add this code at the top of the file even before starting html code.

After adding php code into our table this is what it looks like

simple php file upload script

You can see the output of view table with records.

3. Deleting Uploaded Files From Server Using PHP File

Here I’m going to delete file from the database and also from the server uploads directory with jQuery popup confirmation box.

Before even creating the delete.php file. First, we need to work a little bit on the view.php file.

We need to add jQuery Code for popup confirmation message on click event. This on click event, we will add it anchor tag.

Update the Delete anchor tag code with this below code in view.php file

Next, we need to create delete.php file, that should delete the file on server and also the MySQL database record.

In delete.php file, we need to get the file location information from database based on the id. We will delete this from server, Upon successful deletion of the file, we will delete the record and also redirect the user to view.php file with updated list of files.

First we will check the id value is set and not empty. If it’s empty we will redirect user to view.php else we will process the code to delete the file.

Next we need to delete the record from the database, we will delete the row from datbase on successful deletion of file. Upon successful deletion of row from database we will redirect user to view.php

Complete Code of all the Files

I hope this file upload script using PHP & MySQL, helped you. If you have any problem in implementing this script, let me know through the comment form below.

code for file upload in php with mysql
Vivek Vengala

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: