64

Ajax File Upload with PHP, HTML5 File API and jQuery

Ajax file uploader is useful web tool, which is actually very easy to create with jQuery and PHP, and with new HTML5 FileReader API support, we can do client side file validation before even uploading the file to server. Today we are going to combine all these web features to make an Ajax based file uploader, which can be easily integrated into any HTML forms.  

Ajax file upload

Upload Form Markup

HTML contains file input field, upload button, progress bar and DIV element to output server responses. Only thing to notice here is “multipart/form-data” attribute, it is a data encoding method available in HTML and we always need to include this attribute in our HTML form tag, when creating file upload forms.

 
1
2
3
4
5
6
7
<form action="processupload.php" method="post" enctype="multipart/form-data" id="MyUploadForm">
<input name="FileInput" id="FileInput" type="file" />
<input type="submit"  id="submit-btn" value="Upload" />
<img src="images/ajax-loader.gif" id="loading-img" style="display:none;" alt="Please Wait"/>
</form>
<div id="progressbox" ><div id="progressbar"></div ><div id="statustxt">0%</div></div>
<div id="output"></div>

jQuery and HTML5 File API

Since we are using jQuery, I’ve included jQuery form plugin in our upload form to make things easier. It offers so many options to manipulate form with minimal code, which is great thing, because we can focus on other things like HTML5 File API. The FileReader is a powerful new feature of HTML5, which lets us read the contents of files locally, validating file is easier than ever. Earlier people used different methods to achieve the same, but it was a daunting task.

We only need these few lines of jQuery code to actually send our file to server using Ajax, with jQuery Form plugin included in our page. No more code is required, but as you can see it also accepts various options such as “beforeSubmit“, “success“, “uploadProgress” etc. (You can find more options here). which can greatly extend the functionality of our form, we can just create block of functions and call them within the option.

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$(document).ready(function() {
var options = {
    target:   '#output',   // target element(s) to be updated with server response
    beforeSubmit:  beforeSubmit,  // pre-submit callback
    success:       afterSuccess,  // post-submit callback
    uploadProgress: OnProgress, //upload progress callback
    resetForm: true        // reset the form after successful submit
};
       
 $('#MyUploadForm').submit(function() {
    $(this).ajaxSubmit(options);           
    return false;
});
});

Checking HTML5 File API Browser support

Let’s write a beforeSubmit function, which will be called before uploading the file to server, and it is our important piece of code here, because we are going to use HTML File API in it, which will validate the file size and type locally. Since more and more browsers now support HTML FileReader, I think we should definitely use it more often in our upload forms.

 
1
2
3
4
5
6
7
8
9
10
11
12
function beforeSubmit(){
   //check whether client browser fully supports all File API
   if (window.File && window.FileReader && window.FileList && window.Blob)
    {

        }
        else
    {
       //Error for older unsupported browsers that doesn't support HTML5 File API
       alert("Please upgrade your browser, because your current browser lacks some new features we need!");
    }
}

Checking File size and type before upload using HTML5 File API

The following code accesses the browser File API support and checks whether the user is trying to upload right file type and its size is under the allowed limit. Older browsers will get an error message asking to upgrade their browser.

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function beforeSubmit(){
   //check whether client browser fully supports all File API
   if (window.File && window.FileReader && window.FileList && window.Blob)
    {
       var fsize = $('#FileInput')[0].files[0].size; //get file size
           var ftype = $('#FileInput')[0].files[0].type; // get file type
        //allow file types
      switch(ftype)
           {
            case 'image/png':
            case 'image/gif':
            case 'image/jpeg':
            case 'image/pjpeg':
            case 'text/plain':
            case 'text/html':
            case 'application/x-zip-compressed':
            case 'application/pdf':
            case 'application/msword':
            case 'application/vnd.ms-excel':
            case 'video/mp4':
            break;
            default:
             $("#output").html("<b>"+ftype+"</b> Unsupported file type!");
         return false
           }
   
       //Allowed file size is less than 5 MB (1048576 = 1 mb)
       if(fsize>5242880)
       {
         alert("<b>"+fsize +"</b> Too big file! <br />File is too big, it should be less than 5 MB.");
         return false
       }
        }
        else
    {
       //Error for older unsupported browsers that doesn't support HTML5 File API
       alert("Please upgrade your browser, because your current browser lacks some new features we need!");
           return false
    }
}

Progress Bar

It’s always a good idea to include a progress bar in our file uploader to track upload progress, since we are dealing with different files and unlike images, other files can be bigger in size and can take quite a long time to upload. The jQuery form plugin passes several progress information to our function (If supported by browser), which we will capture and use to animate our progress bar.

 
1
2
3
4
5
6
7
8
9
10
11
function OnProgress(event, position, total, percentComplete)
{
    //Progress bar
    $('#progressbox').show();
    $('#progressbar').width(percentComplete + '%') //update progressbar percent complete
    $('#statustxt').html(percentComplete + '%'); //update status text
    if(percentComplete>50)
        {
            $('#statustxt').css('color','#000'); //change status text to white after 50%
        }
}

Server side PHP

Once the file is successfully uploaded to server via the HTTP POST method, we can access the file information using PHP code. The information we need here are file name, type and size, using the information here too we double check for valid file size and type. And then using PHP move_uploaded_file function, we will move the file to a new location, then we can just add mySQL code to store information in the database.

Here’s the complete PHP code which does the server side task, you need to place this file along with upload HTML form page we created earlier, just point your Ajax form to this file.

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
if(isset($_FILES["FileInput"]) && $_FILES["FileInput"]["error"]== UPLOAD_ERR_OK)
{
    ############ Edit settings ##############
    $UploadDirectory    = '/home/website/file_upload/uploads/'; //specify upload directory ends with / (slash)
    ##########################################
   
    /*
    Note : You will run into errors or blank page if "memory_limit" or "upload_max_filesize" is set to low in "php.ini".
    Open "php.ini" file, and search for "memory_limit" or "upload_max_filesize" limit
    and set them adequately, also check "post_max_size".
    */

   
    //check if this is an ajax request
    if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])){
        die();
    }
   
   
    //Is file size is less than allowed size.
    if ($_FILES["FileInput"]["size"] > 5242880) {
        die("File size is too big!");
    }
   
    //allowed file type Server side check
    switch(strtolower($_FILES['FileInput']['type']))
        {
            //allowed file types
            case 'image/png':
            case 'image/gif':
            case 'image/jpeg':
            case 'image/pjpeg':
            case 'text/plain':
            case 'text/html': //html file
            case 'application/x-zip-compressed':
            case 'application/pdf':
            case 'application/msword':
            case 'application/vnd.ms-excel':
            case 'video/mp4':
                break;
            default:
                die('Unsupported File!'); //output error
    }
   
    $File_Name          = strtolower($_FILES['FileInput']['name']);
    $File_Ext           = substr($File_Name, strrpos($File_Name, '.')); //get file extention
    $Random_Number      = rand(0, 9999999999); //Random number to be added to name.
    $NewFileName        = $Random_Number.$File_Ext; //new file name
   
    if(move_uploaded_file($_FILES['FileInput']['tmp_name'], $UploadDirectory.$NewFileName ))
       {
        // do other stuff
               die('Success! File Uploaded.');
    }else{
        die('error uploading File!');
    }
   
}
else
{
    die('Something wrong with upload! Is "upload_max_filesize" set correctly?');
}

That’s it, I have tried to make this tutorial very simple, you can download the sample files from below and continue with testing. The demo you’ll see is from different tutorial, but works similar way and it only allows image files. If you have any suggestions please go ahead and leave your valuable comments, good luck!

Download Demo

Related Articles:

64 Comments Add Comment

Comments Navigation : 1 2
  1. It works nicely until I add another submit script (the image uploader) on the same page.
    $(‘#MyUploadForm2′).submit(function() {
    $(this).ajaxSubmit(options);
    Error: Object doesn’t support property or method ‘ajaxSubmit’
    ..and the HTTP_X_REQUESTED_WITH is not set.
    if (!isset($_SERVER[‘HTTP_X_REQUESTED_WITH’])){
    die();
    }
    Is this possible because of jQuery conflict between the two seperate uploads and can you please advice on solutuion? Thanks.

    Add Reply
    • The object, errors always tell you whats wrong. is #MyUploadForm2 goes direct to the form? How? Is your form setup like ?

  2. i have used your code with file uploder ajax-jquery but i found the image limit below 5mb jpg images.. because of lack of knowledge in ajax n jquery i cant correct that.. please do help me to correct the code…

    Add Reply
    • You need to check your PHP config file. It will limit the file size by a default value, 5MB sounds about right. You can increase the limit, just do a search on increasing the limit in the config file.

Comments Navigation : 1 2

 

Use Gravatar for Comment Pic | Start a topic for crucial discussion.


 Notify me of followup comments via e-mail. You can also subscribe without commenting.

Go Top ↑