Skip to main content

PHP - Get File Size From Directory

In this tutorial we will create a Get File Size From Directory using PHP. This code will display the actual filze size of a file when user click the button in the table row. This code use a simple href or hypertext reference to call a php function that send the file data through url which display the file size of a file using filesize() and by adding the file path as a parameter.This is a user-friendly kind of program feel free to modify it.

We will be using PHP as a scripting language that manage a database server to handle a bulk of data per transaction. It describe as an advance technology that manage both server and control-block of your machine.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server

And, this is the link for the bootstrap that i used for the layout design

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. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Get File Size From Directory</h3>
  16. <hr style="border-top:1px dottec #ccc;"/>
  17. <div class="col-md-4">
  18. <div class="form-inline">
  19. <form method="POST" action="create_file.php">
  20. <label style="font-size:18px;">Filename:</label>
  21. <input type="text" name="filename" class="form-control" required="required"/>
  22. <br /><br />
  23. <label>Enter a Text</label>
  24. <textarea name="content" style="width:100%; height:100px; resize:none;"></textarea>
  25. <br /><br />
  26. <center><button class="btn btn-primary" name="create">Create File</button></center>
  27. </form>
  28. </div>
  29. </div>
  30. <div class="col-md-8">
  31. <table class="table table-bordered">
  32. <thead class="alert-info">
  33. <tr>
  34. <th>File Name</th>
  35. <th>Action</th>
  36. </tr>
  37. </thead>
  38. <tbody>
  39. <?php
  40. $full_path = "files";
  42. $dir = opendir($full_path);
  44. while(($file = readdir($dir)) !== FALSE){
  45. if($file == "." || $file == "..")
  47. continue;
  48. ?>
  49. <tr>
  50. <td><?php echo $file?></td>
  51. <td><a class="btn btn-primary btn-sm" href="get_filesize.php?file_name=<?php echo $file?>">Get Filesize</a></td>
  52. </tr>
  53. <?php
  55. }
  57. closedir($dir);
  58. ?>
  60. </tbody>
  61. </table>
  62. </div>
  63. </div>
  64. </body>
  65. </html>

Creating the Adding Function

This code contains the add function of the application. This code will add a new file when user supply the require information. To do that just copy and write this block of codes inside the text editor, then save it as create_file.php.

  1. <?php
  2. if(ISSET($_POST['create'])){
  3. $content = $_POST['content'];
  4. $file_name = $_POST['filename'];
  5. $path = "files";
  6. $file = fopen($path."/".$file_name.".txt", 'w');
  7. fwrite($file, $content);
  8. fclose($file);
  9. header("location:index.php");
  10. }
  11. ?>

Creating the Main Function

This code contains the main function of the application. This code will display the actual file size of a file when the button is clicked. To do that just copy and write this block of codes inside the text editor, then save it as get_filesize.php.

  1. <?php
  2. if(ISSET($_REQUEST['file_name']))
  4. $file = "files/".$_REQUEST['file_name'];
  6. $size = filesize($file);
  8. $sizeInKB = ($size / 1024);
  11. $total_size = number_format($sizeInKB, 2);
  13. echo "<script>alert('The filesize is: ".$total_size."KB')</script>";
  14. echo "<script>window.location='index.php'</script>";
  15. ?>

There you have it we successfully created Get File Size From Directory 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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.