get system free disk space in php

You can use the df command in PHP to get the amount of free disk space available in a Linux or Unix-based system. This command returns an array containing information about the current disk usage of each mounted file system.

Here is an example of how to use the df command in PHP to get the amount of free disk space on the root partition:

main.php
<?php
$df = shell_exec('df -h /');
$df = preg_split('/\s+/', $df);
$free_space = $df[11];
echo "Free disk space: $free_space";
?>
130 chars
7 lines

This code prints the amount of free disk space on the root partition in a human-readable format (e.g. "50G"). If you want to get the free space in bytes, you can modify the df command to use the -B option and parse the output accordingly:

main.php
<?php
$df = shell_exec('df -B 1 /');
$df = preg_split('/\s+/', $df);
$free_bytes = $df[11] * 1024;
echo "Free disk space: $free_bytes bytes";
?>
145 chars
7 lines

Note that using shell commands in PHP can pose security risks if not done carefully, so make sure to sanitize any user input before using it in the shell_exec function.

gistlibby LogSnag