- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP exit()
- PHP exit() vs. break
- PHP isset()
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP mysqli Tutorial
- PHP mysqli Tutorial
- PHP and MySQL Setup
- PHP mysqli: Create Database
- PHP mysqli: Create Table
- PHP mysqli: Insert Record
- PHP mysqli: Update Record
- PHP mysqli: Fetch Record
- PHP mysqli: Delete Record
- PHP mysqli: SignUp Page
- PHP mysqli: LogIn Page
- PHP mysqli: Store User Data
- PHP mysqli Functions
- PHP mysqli_connect()
- PHP mysqli_close()
- PHP mysqli_connect_errno()
- PHP mysqli_connect_error()
- PHP mysqli_query()
- PHP mysqli_fetch_row()
- PHP mysqli_fetch_assoc()
- PHP mysqli_fetch_array()
- PHP mysqli_free_result()
- PHP mysqli_error()
- PHP mysqli_prepare()
- PHP mysqli_stmt_bind_param()
- PHP mysqli_stmt_execute()
- PHP mysqli_stmt_fetch()
- PHP mysqli_stmt_store_result()
- PHP mysqli_stmt_num_rows()
- PHP mysqli_stmt_bind_result()
- PHP mysqli_stmt_get_result()
- PHP mysqli_result class
- PHP mysqli_report()
- PHP error_reporting()
- PHP mysqli_real_escape_string()
- PHP htmlspecialchars()
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP header()
- PHP getallheaders()
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP filesize(): Get the size of the file
The PHP filesize() function returns the size of a specified file in bytes. For example:
<?php echo filesize("codescracker.txt"); ?>
The output produced by the above PHP example on the filesize() function is:
And here is the snapshot of the file codescracker.txt, used in the above example:
PHP filesize() Syntax
The syntax of the filesize() function in PHP is:
filesize(fileName)
PHP gets the file size in KB using filesize()
The following PHP code finds and prints the size of a file in terms of kilobytes (KB).
<?php $myfile = "codescracker.txt"; $x = filesize($myfile); $x = $x/1024; echo "<p>Size of File, $myfile is $x KB</p>"; ?>
The output produced by the above PHP example, to find and print the size of a file in KB, is:
If you want to remove all the digits after the decimal, then put (int) before the size, in a similar way as done in the following program:
<?php $myfile = "favicon.ico"; $x = filesize($myfile); $x = (int)($x/1024); echo "<p>Size = $x KB</p>"; ?>
Now the output should be "Size = 30 KB," because the file favicon.ico is 30 KB. I've not used the codescracker.txt file in the example above, as the size of that file is 46 bytes, which will give 0 KB as output.
PHP gets the file size in MB using filesize()
The following PHP code finds and prints the size of a file in terms of megabytes (MB).
<?php $myfile = "C:/xampp/xampp-control.exe"; $x = filesize($myfile); $x = (int)($x/1024/1024); echo "<p>Size = $x MB</p>"; ?>
The output produced by this PHP example, which prints the size of the xampp-control.exe file available in the C:/xampp/ directory, is shown in the snapshot given below:
And here is the snapshot of the file along with the directory used in the above example:
PHP gets the file size in GB using filesize()
The following PHP code finds and prints the size of a file in terms of gigabytes (GB).
<?php $myfile = "F:/sw/os/windows64.iso"; $x = filesize($myfile); $x = (int)($x/1024/1024/1024); echo "<p>Size = $x GB</p>"; ?>
Since the size of file windows64.iso is 5.1GB, therefore after removing ".1," you will get Size = 5 GB as output.
Advantages of the filesize() function in PHP
- The filesize() function is straightforward and only requires one line of code to determine the size of a file.
- The filesize() function is extremely efficient because it only retrieves the file's size rather than reading its entire contents.
- The filesize() function consumes little memory, making it appropriate for use with large files.
- The filesize() function accurately represents the file size, including any hidden characters or whitespace.
Disadvantages of the filesize() function in PHP
- The filesize() function provides only the file's size and no other information regarding its contents.
- If the user controls the file being checked, the filesize() function poses a security risk. This may allow malicious users access to sensitive file information on the server.
- If the file cannot be read or if there are other problems with the file, the filesize() function does not provide detailed error messages.
- Depending on the platform, the filesize() function may behave differently on various platforms or operating systems, which may impact the accuracy of the file size measurement.
« Previous Tutorial Next Tutorial »