- 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 disk_free_space(): Find Amount of Free Disk Space
The PHP disk_free_space() function is used when we need to find the free space available on a specified disk or filesystem. The space returned using this function will be in bytes by default. For example:
<?php $space = disk_free_space("C:"); echo $space; ?>
The output of the previous example is:
That is, 67981537280 bytes. Here is a snapshot of the "C" drive of my computer system:
PHP Find Amount of Free Disk Space in GB
To modify the above program in a way to print the amount of free space available on the "C" drive in "GB," then use the following example:
<?php $spaceBytes = disk_free_space("C:"); $spaceKb = $spaceBytes/1024; $spaceMb = $spaceKb/1024; $spaceGb = $spaceMb/1024; echo "<p>Free Space available in <b>C</b> Drive is <b>$spaceGb</b> GB</p>"; ?>
Now the output should be:
To remove all digits after decimals, use (int) before $spaceGb. For example:
<?php $spaceBytes = disk_free_space("C:"); $spaceGb = $spaceBytes/1024/1024/1024; $spaceGb = (int)$spaceGb; echo "<p><b>C</b> Drive Free Space = <b>$spaceGb</b> GB</p>"; ?>
Now the output should be:
PHP disk_free_space() Syntax
The syntax of the disk_free_space() function in PHP is:
disk_free_space(x)
The x parameter is required and refers to the filesystem or disk whose free space we need to find or see.
Advantages of the disk_free_space() function in PHP
- The path to the disk or partition you want to check is the only input needed for this very straightforward function.
- The feature is accessible across a variety of platforms and is compatible with both Windows and Unix-based operating systems.
- It can be helpful for keeping track of how much space is left on a disk or partition, which is crucial for making sure that processes or applications don't run out of room.
Disadvantages of the disk_free_space() function in PHP
- The "disk_free_space()" function does not return any other data, such as the total size of the disk, the amount of used space, or the percentage of free space; it only returns the amount of free disk space.
- The function's portability may be restricted by the fact that it might not operate on some systems or that it might need specialized access rights to disk data.
- It may not be possible to diagnose the issue if the function returns a false value if it is unable to retrieve disk information. Furthermore, if the function fails, some versions of PHP might not produce an error message.
- If the function is used to access sensitive data stored on a disk, improper use could leave it open to security flaws or attacks.
« Previous Tutorial Next Tutorial »