How to get the size of a disk partition via PHP file system functions
Through the file system function not only can the directory, the file operation, but also can obtain the directory, the file related information, then can obtain the disk partition size!
Gets the size of the disk partition applied is the DISK_TOTAL_SAPCE () function, and the Disk_free_space () function is applied to get the remaining space of the disk partition.
Disk_total_space () function: Gets the disk size of a directory in the following syntax format:
float disk_total_space (String directory)
The function returns all the bytes that are differentiated by the corresponding file system or disk, based on a directory string supplied by the parameter directory.
Attention:
This function does not work on remote files, its detected files must be accessed through the server's file system!
Disk_free_space () function: Gets the free space for a directory in the following syntax format:
float disk_free_space (String directory)
The parameter directory is used to specify file system or disk differentiation!
Description
This function returns the total size of the partition in which the directory is located, so give the same disk partition a different directory as the parameters of the result is exactly the same!
Application examples
This example applies the Disk_total_space () function to get the disk partition size. The specific code is as follows:
(1) Create a PHP file, add a form, set a text box, submit a button, use the Post method, and tell the submission to this page.
(2) Obtain the directory path submitted by the form by $_post, first determine whether the obtained directory is reasonable. The obtained string is then encoded by the ICONV () function, and then the obtained string is intercepted. Get the referee partition where the directory is located, and apply the Disk_total_space () function to get the size of the disk partition, and finally, apply the Opendir () function and the Readdir () function to read the contents of the submission directory, as follows:
<form name= "Form1" action= "6.php" method= "POST" > File name: <input name= "file_name" type= "text" > <in Put type= "Submit" name= "Submit" value= "Submission" ></form><?phpheader ("content-type:text/html; Charset=utf-8 "), if ($_post[' file_name ')) {if ($_post[' file_name ']!=" "&& is_file ($_post[' file_name ']) = = False) {$file _name = iconv ("Utf-8", "gb2312", $_post[' file_name ']); Encode format conversion if (file_exists ($file _name)) {//Determine if the directory exists $len = Strripos ($file _name, " :"); Intercept string $dir = substr ($file _name,0, $len + 1); Get the disk $filesize _z = Disk_total_space ($dir) where the submission directory resides. Get the directory size $filesize _z = Number_format ($filesize _z/(1024*1024*1024), 2, ".", "" "); The format of the number $filesize _s = Disk_free_space ($dir); Get the remaining space of the referee $filesize _s = Number_format ($filesize _s/(1024*1024*1024), 2, ".", "" "); The format of the byte echo "Local Disk (". $dir.") Total size:". $filesize _z. " GB Free space: ". $filesize _s." GB <br><br> "; echo $_post[' file_name ']. " Content under the directory: "." <br> "; $i = 0; $list = Opendir ($file _name); Open Directory while ($read _list = Readdir ($list)) {//Read directory $i + +; echo "$i:". Iconv ("gb2312", "Utf-8", $read _list). " <br> "; Content in output directory} closedir ($list); About the directory}else{echo "<script>alert" (' Directory does not exist! ');</script> "; }}else{echo "<script>alert (' Please enter the correct directory path! ');</script> ";}}
The above output results are as follows:
Description
The Strripos () function is used to determine where the specified string last appears in another string. This function allows you to get to the last occurrence of the specified string A in another string B, whose return value is int, which allows you to intercept string B as a split point with string A, which you need to apply to the substr () function when performing the intercepted operation.
$len = Strripos ($file _name, ":"); Intercept string $dir = substr ($file _name,0, $len + 1); Get the disk where the commit directory resides