PHP - Simple File Size Validator

Submitted by: 
Language: 
Visitors have accessed this post 187 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  13. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  14. </a> </div>
  15. </nav>
  16. <div class="col-md-3"></div>
  17. <div class="col-md-6 well">
  18. <h3 class="text-primary">PHP - Simple File Size Validator</h3>
  19. <hr style="border-top:1px dotted;"/>
  20.  
  21. <div class="col-md-2"></div>
  22. <div class="col-md-8">
  23. <div class="form-group">
  24. <input type="file" id="file" class="form-control"/>
  25. </div>
  26. <span id="result" style="display:none;">Hello World</span>
  27. <center><button style="width:200px;" id="check" class="btn btn-success">Check</button></center>
  28. </div>
  29. </div>
  30. <script src="js/jquery-3.2.1.min.js"></script>
  31. <script src="js/script.js"></script>
  32. </body>
  33. </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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.