Get File size and File Extension in PHP Free Source code and Tutorial
Introduction
Table of Contents
The filesize function in PHP returns the size of a file in bytes. To use the function, you simply need to provide the name of the file as the parameter. The function will return the size of the file in bytes.
Another part of this tutorial is to get the file extension of the selected file. There are a few ways to get the file extension in PHP. One way is to use the function pathinfo(), which returns information about a file, including the file extension.
Objectives
By the end of this tutorial, you will be able to:
- Create a PHP script that allows the user to select a file and display the file size and file extension.
- Use filesize() function to get the file size of the selected file.
- Use pathinfo() function to get the file extension of the selected file.
- To integrate and apply the source code in your projects
Relevant Source code
In this tutorial, we will learn how to create a PHP script that will display the file size and file extension of the selected or uploaded file.
The following are the requirements for this tutorial:
- XAMPP
- Text editor (VS Code, Sublime, Brackets), download and install a text editor of your choice
Line 24 – 36 – this is the scope of the PHP script that will display the file size and file extension of the selected or uploaded file.
After the user have selected or uploaded the file and clicks the submit button, the script will then execute and it will display the file size and file extension.
Line 27 – the filesize() function in PHP will display the file size of the file in bytes.
Parameters: The filesize() function in PHP has only one parameter. That parameter is the file being selected or uploaded.
Line 31-33 – we have created a function that returns the file information about the file.
pathinfo() function in PHP has two parameters. The first parameter refers to the path to be checked and the second parameter specifies which array element to return. In this case we will use PATHINFO_EXTENSION since we want to get and display the file extension of the uploaded file.
Line 34 – we will now display the file extension of the selected or uploaded file.
<?php if(isset($_POST['submit'])){ $filename = $_POST['file']; echo "<h4>Size in Bytes: </h4>". filesize($filename)." b<br>"; echo "<h4>Size in Kilobytes: </h4>". filesize($filename)/1000 ." kb<br>"; echo "<h4>Size in Megabytes: </h4>". filesize($filename)/1000000 ." mb<br>"; function getExtension($filename){ return pathinfo($filename, PATHINFO_EXTENSION); } echo "<h4>File Extension: </h4>.". getExtension($filename); } ?>
Video Demo
Summary
To sum it up, we have learned how use PHP functions that allow us to get the file size and file extension of the selected file. In this article, you can download the source code for your reference. For the complete reference please watch the video presentation on how to create a PHP script that will display the file size and file extension of the selected file.
We hope you found this tutorial to be helpful! Wishing you the best of luck with your projects! Happy Coding!
You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.
Hire our team to do the project.
Related Topics and Articles:
How to Generate Random Password in PHP Free Source code and Tutorial
Email Validation in PHP Free Source code and Tutorial
Law Office Management Information System in Bootstrap and PHP Script