The disk_total_space function in PHP is used to return the total space of the disk partition where a specified directory resides. Its basic syntax is as follows:
<span><span><span class="hljs-title function_ invoke__">disk_total_space</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$directory</span></span><span>): </span><span><span class="hljs-keyword">float</span></span><span>|</span><span><span class="hljs-literal">false</span></span><span>
</span></span>
Here, the $directory parameter specifies the path whose disk space you want to check. If successful, the function returns a floating-point number representing the total space (in bytes). If an error occurs or the disk space cannot be accessed, the function returns false.
If the PHP script does not have sufficient permissions to read the file system information of the specified directory, disk_total_space will return false. This usually happens in the following cases:
The current PHP process lacks adequate file system access permissions.
The directory being accessed is restricted (e.g., system folders or certain protected directories).
The disk_total_space function requires a valid, mounted directory path. If you pass a non-existent path or a path pointing to an unmounted partition, the function will return false.
Some special file systems (like network-mounted storage or non-standard mount types) may prevent disk_total_space from working properly. PHP does not fully support certain file system types, especially on specific operating systems or environments.
In certain environments, such as shared hosting, permissions to query disk space may be restricted, and PHP scripts might not be able to access disk information. These restrictions are often imposed by system administrators for security reasons.
Ensure the user running the PHP script has sufficient permissions to access the file system. You can use the ls -l command to check directory permissions and confirm that the PHP script's user has adequate read access. If permissions are insufficient, you can modify them using the chmod command.
<span><span><span class="hljs-built_in">chmod</span></span><span> 755 /path/to/directory
</span></span>
Make sure the path passed to disk_total_space is valid and exists. You can use the is_dir() function to check if the path exists and is a valid directory. For example:
<span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-title function_ invoke__">is_dir</span></span><span>(</span><span><span class="hljs-variable">$directory</span></span><span>)) {
</span><span><span class="hljs-variable">$totalSpace</span></span><span> = </span><span><span class="hljs-title function_ invoke__">disk_total_space</span></span><span>(</span><span><span class="hljs-variable">$directory</span></span><span>);
</span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-variable">$totalSpace</span></span><span> === </span><span><span class="hljs-literal">false</span></span><span>) {
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Unable to retrieve disk space information."</span></span><span>;
} </span><span><span class="hljs-keyword">else</span></span><span> {
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Total disk space: "</span></span><span> . </span><span><span class="hljs-variable">$totalSpace</span></span><span> . </span><span><span class="hljs-string">" bytes"</span></span><span>;
}
} </span><span><span class="hljs-keyword">else</span></span><span> {
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"The specified path is invalid or not a directory."</span></span><span>;
}
</span></span>
Ensure that your PHP environment supports the file system you are using. You can check the system’s file system type and make sure it is a standard local storage type. If it is a special network file system (like NFS), you may need to consult documentation or contact the system administrator to understand any limitations.
If disk_total_space still cannot retrieve disk space, consider using operating system commands to get disk information. For example, on Linux, you can use the df command to get disk space data:
<span><span><span class="hljs-variable">$df</span></span><span> = </span><span><span class="hljs-title function_ invoke__">shell_exec</span></span><span>(</span><span><span class="hljs-string">'df /path/to/directory'</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$df</span></span><span>;
</span></span>
On Windows, you can use the wmic command to get disk information:
<span><span><span class="hljs-variable">$wmic</span></span><span> = </span><span><span class="hljs-title function_ invoke__">shell_exec</span></span><span>(</span><span><span class="hljs-string">'wmic logicaldisk get size,freespace,caption'</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$wmic</span></span><span>;
</span></span>
These commands can bypass PHP limitations and retrieve disk space directly from the operating system level.
When disk_total_space fails to retrieve disk space, first check for permission issues and path validity. If the problem persists, consider alternative methods such as calling operating system commands to obtain disk information. These approaches help effectively diagnose and resolve issues with the disk_total_space function.