In this tutorial, we will create a Get File Size From Directory using PHP. This code will display the actual file size of a file when the user clicks the button in the table row. This code uses a simple href or hypertext reference to call a PHP function that sends the file data through URL, which displays 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 manages a database server to handle a bulk of data per transaction. It describes as an advanced technology that manages both server and control-block of your machine.
Getting Started:
First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the 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 a copy of the way and write it into your text editor, 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";
$dir = opendir($full_path);
while(($file = readdir($dir)) !== FALSE){
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
}
closedir($dir);
?>
</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.
<?php
if(ISSET($_POST['create'])){
$content = $_POST['content'];
$file_name = $_POST['filename'];
$path = "files";
$file = fopen($path."/".$file_name.".txt", 'w');
fwrite($file, $content);
fclose($file);
header("location:index.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
if(ISSET($_REQUEST['file_name']))
$file = "files/".$_REQUEST['file_name'];
$size = filesize($file);
$sizeInKB = ($size / 1024);
$total_size = number_format($sizeInKB, 2);
echo "<script>alert('The filesize is: ".$total_size."KB')</script>";
echo "<script>window.location='index.php'</script>";
?>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!















