PHP - Get File Size From Directory

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

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 https://www.apachefriends.org/index.html.

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


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.