In this tutorial, we are going to see how we can upload files to the server using PHP. Here we are going to upload image, video and music files.
Along with the tutorial, we are also going to explain every step in detail.

This project won’t have any styling. Here our only goal is to upload files to the server. For that, we are using a local server to host the files.

How it works

There will be a browse button, clicking on which the user can browse the files. And there will be a submit upload button. Clicking on which the file will be uploaded.

The user can select any image, music or video files. All these files won’t be saved in the same folder. There will be different directories for audio, image and video files.

There will be a size limit for all these files. The Same file can’t be uploaded multiple times. However, one can change the file name and upload it more than once.

If you want to allow the user to upload same files more than once without manually editing the file name you can refer to Upload image file and display in HTML.

How to do it

First of all, create a folder and name it “phpUploadFile”. Open the folder and create two files “index.php” and “upload.php”.

Now create a folder “uploads”. This is the folder where the uploaded files will be stored. Open the uploads folder and make another three folders “image”, “video” and  “audio”.

fileUpload-php-walkytechy

fileUpload-php-walkytechy

After creating all the files and folders, copy and paste the below code into the “index.php”.

From the above code, you can see we have an extra attribute “enctype”.
Note that we can use this enctype attribute only when the form method is “POST”.

Now that our index.php page is ready, it is the time to code the upload.php file. Copy and paste the below code into the upload.php file.

Note: By default the file upload limit 2MB. So before you run the above code you have to change the maximum upload limit.

Locate php.ini file and change the default file size limit

Now run the index.php page and try to upload files. Click on “Choose File” and select the file you want to upload. After selecting the file click “Upload”. The file will be uploaded to the specific folder.
From the above code, you can see we have set limits for all the files. 2MB for images,  1000MB for videos and 10MB for audio files. Yoy can change the size according to your requirement.

Code explanation
Here “myFile” is the name of our file input type. See index.php page. Since the input type is “file”, we have to use the global variable $_FILES.

So the above line gives us the selected file name. Suppose our file name is walkytechy.jpg. So now
$myFile = walkytechy.jpg.

This is the temporary location of the file. We will move the file from this temporary location to the “uploads/” directory.

In this project, we have a different folder for different file types. To store different file type to different folder, we have to identify the file type. We can identify the uploaded file type by their extension. So we need the above code snippet to identify the extension. Now $ext = jpg.

After getting the extension it is easy to identify the file type.
If the file is an image type, set the destination path as:

If it is a video file, set the path as:

And if the file is an audio file, set the path as:

We have used the most common file extension for checking the type. You get more extension from Fileinfo.

As you can see from upload.php we have checked if the file already exists or not. If the file is already in the destination folder, we are showing a simple warning with an alert box.

Here the $path is the destination path. We are also checking if the uploaded file size exceeds the limit.

[“myFile”][“size”] is used to get the size of the selected file. After getting the file size we can compare if the size exceeds the 1000MB limit. You can change the max size limit according to your need.
Here 1000*MB means 1000MB. MB = 1MB or 1024KB or 1024*1024bytes. You can also write 1000*1048576. Which is 1048576000 bytes or 1000MB. For simplicity, we have used define function, which simply defines a constant value.

At last, we have used move_uploaded_file.

This is used to move the file from temporary path to the destination path.

 

Uploading files to the server using PHP
Tagged on:

Leave a Reply

Your email address will not be published. Required fields are marked *