PHP - Simple File Size Validator

In this tutorial we will create a Simple File Size Validator using PHP. PHP is a server-side scripting language designed primarily for web development. PHP is a server-side scripting language designed primarily for web development. It is mostly used by a newly coders for its user friendly environment. So let's now do the coding...

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <head>
  5.                         <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6.                         <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  7.                 </head>
  8.         </head>
  9. <body>
  10.         <nav class="navbar navbar-default">
  11.                 <div class="container-fluid">
  12.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  13.                 </div>
  14.         </nav>
  15.         <div class="col-md-3"></div>
  16.         <div class="col-md-6 well">
  17.                 <h3 class="text-primary">PHP - Simple File Size Validator</h3>
  18.                 <hr style="border-top:1px dotted;"/>
  19.                
  20.                 <div class="col-md-2"></div>
  21.                 <div class="col-md-8">
  22.                         <div class="form-group">
  23.                                 <input type="file" id="file" class="form-control"/>
  24.                         </div>
  25.                         <span id="result" style="display:none;">Hello World</span>
  26.                         <center><button style="width:200px;" id="check" class="btn btn-success">Check</button></center>
  27.                 </div> 
  28.         </div>
  29. <script src="js/jquery-3.2.1.min.js"></script>
  30. <script src="js/script.js"></script>
  31. </body>
  32. </html>

Creating the Main Function

This code contains the main function of the application. This code will validate the uploaded to check if it can be store to the database. To do that just kindly copy and write these block of codes inside the text editor then save it as shown below. file_validator.php
  1. <?php
  2.         if($_FILES['file']['size'] < 10485760){
  3.                 echo "success";
  4.         }else{
  5.                 echo "error";
  6.         }
  7. ?>
script.js
  1. $(document).ready(function(){
  2.         $('#check').on('click', function(){
  3.                 var file = $('#file');
  4.                 var file_length = file[0].files.length;
  5.                 var file_data = file.prop('files')[0];
  6.                 if(file_length > 0){
  7.                         var form_data = new FormData();
  8.                         form_data.append('file', file_data);
  9.                         $('#result').css('display', '');
  10.                         $('#result').html("<center><label class='text-success'>Checking....</label></center>");
  11.                         $('#check').attr('disabled', 'disabled');
  12.                         setTimeout(function(){
  13.                                 $.ajax({
  14.                                         url: 'file_validator.php',
  15.                                         type: 'POST',
  16.                                         processData: false,
  17.                                         contentType: false,
  18.                                         data: form_data,
  19.                                         success: function(data){
  20.                                                 if(data == "success"){
  21.                                                         $('#result').html("<center><label class='text-primary'>You can now upload this file</label></center>");
  22.                                                         $('#check').removeAttr('disabled');
  23.                                                 }else if(data == "error"){
  24.                                                         $('#result').html("<center><label class='text-danger'>File must be less than 10 mb</label></center>");
  25.                                                         $('#check').removeAttr('disabled');
  26.                                                 }
  27.                                         }
  28.                                 });    
  29.                         }, 3000);      
  30.                                
  31.                 }else{
  32.                         $('#result').html("<center><label class='text-danger'>No File Selected</label></center>");
  33.                 }
  34.         });
  35.        
  36.        
  37. });
There you have it we successfully created Simple File Size Validator using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment