PHP - Get File Size From Directory
Submitted by razormist on Wednesday, October 2, 2019 - 14:19.
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.
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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-3"></div>
- <div class="col-md-6 well">
- <h3 class="text-primary">PHP - Get File Size From Directory</h3>
- <hr style="border-top:1px dottec #ccc;"/>
- <div class="col-md-4">
- <div class="form-inline">
- <form method="POST" action="create_file.php">
- <label style="font-size:18px;">Filename:</label>
- <input type="text" name="filename" class="form-control" required="required"/>
- <br /><br />
- <label>Enter a Text</label>
- <textarea name="content" style="width:100%; height:100px; resize:none;"></textarea>
- <br /><br />
- <center><button class="btn btn-primary" name="create">Create File</button></center>
- </form>
- </div>
- </div>
- <div class="col-md-8">
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- <th>File Name</th>
- <th>Action</th>
- </tr>
- </thead>
- <tbody>
- <?php
- $full_path = "files";
- if($file == "." || $file == "..")
- continue;
- ?>
- <tr>
- <td><?php echo $file?></td>
- <td><a class="btn btn-primary btn-sm" href="get_filesize.php?file_name=<?php echo $file?>">Get Filesize</a></td>
- </tr>
- <?php
- }
- ?>
- </tbody>
- </table>
- </div>
- </div>
- </body>
- </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.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.- <?php
- $file = "files/".$_REQUEST['file_name'];
- $sizeInKB = ($size / 1024);
- echo "<script>alert('The filesize is: ".$total_size."KB')</script>";
- echo "<script>window.location='index.php'</script>";
- ?>
Add new comment
- 318 views