Home > Backend Development > PHP Tutorial > How to get the size of disk partition through php file system function

How to get the size of disk partition through php file system function

黄舟
Release: 2023-03-07 19:50:01
Original
3090 people have browsed it

How to get the size of the disk partition through the php file system function

The file system function can not only operate directories and files, but also obtain directories. File-related information, then whether it is possible to obtain the size of the disk partition!

The disk_total_sapce() function is used to obtain the size of the disk partition, and the disk_free_space() function is used to obtain the remaining space of the disk partition.

disk_total_space() function: Get the disk size of a directory. The syntax format is as follows:

float disk_total_space(string directory)
Copy after login

This function returns the corresponding file system or disk distinction based on a directory string provided by the parameter directory. All bytes.

Note:

This function cannot be used on remote files, and the files it detects must pass the server File system access!

disk_free_space() function: Get the available space of a directory. The syntax format is as follows:

float disk_free_space(string directory)
Copy after login

Parameter directory is used to specify the file system or disk distinction!

Explanation:

This function returns the total size of the disk partition where the directory is located. Therefore, if you give different directories of the same disk partition as parameters, the results will be exactly the same!

Application Example

This example uses the disk_total_space() function to obtain the disk partition size. The specific code is as follows:

(1) Create a php file, add a form, set a text box, submit button, use the POST method, and submit to this page.

(2) Obtain the directory path for form submission through $_POST, and first determine whether the obtained directory is reasonable. Then the obtained string is encoded and converted through the iconv() function, and then the obtained string is intercepted. Get the referee partition where the directory is located, and use the disk_total_space() function to get the size of the disk partition. Finally, use the opendir() function and readdir() function to read the contents of the submitted directory. The specific code is as follows:

<form name="form1" action="6.php" method="post">
   文件名称: <input name="file_name" type="text">
           <input type="submit" name="Submit" value="提交">
</form>
<?php
header("Content-Type:text/html; charset=utf-8");
if ($_POST[&#39;file_name&#39;]){
if($_POST[&#39;file_name&#39;]!="" && is_file($_POST[&#39;file_name&#39;])==false){
    $file_name = iconv("utf-8","gb2312",$_POST[&#39;file_name&#39;]);      // 编码格式转换
    if (file_exists($file_name)){                                  //判断目录是否存在
        $len = strripos($file_name,":");                            //截取字符串
        $dir = substr($file_name,0,$len+1);                        //获取提交目录所在磁盘
        $filesize_z = disk_total_space($dir);                       //获取目录大小
        $filesize_z = number_format($filesize_z/(1024*1024*1024),2,".","");    //数字的格式化
        $filesize_s = disk_free_space($dir);                            //获取裁判剩余空间
        $filesize_s =  number_format($filesize_s/(1024*1024*1024),2,".",""); //字节的格式化
        echo "本地磁盘(".$dir.") 总大小:".$filesize_z." GB   可用空间:".$filesize_s."GB <br><br>";
        echo $_POST[&#39;file_name&#39;]."目录下的内容:"."<br>";
        $i = 0;
        $list = opendir($file_name);                                //打开目录
        while($read_list = readdir($list)){                         //读取目录
            $i++;
            echo "$i:".iconv("gb2312","utf-8",$read_list)."<br>";   //输出目录中的内容
        }
        closedir($list);                                            //关于目录
    }else{
        echo "<script>alert(&#39;目录不存在!&#39;);</script>";
    }
}else{
    echo "<script>alert(&#39;请输入正确的目录路径!&#39;);</script>";
}
}
Copy after login

The above output results are as follows:

How to get the size of disk partition through php file system function

Description:

strripos() function Used to determine the last occurrence of a specified string in another string. Through this function, you can get the last position of the specified string A in another string B. The return value is int type. Based on the return value, string B can be intercepted using string A as the dividing point. The substr() function needs to be applied when performing interception operations.

$len = strripos($file_name,":");                            //截取字符串
$dir = substr($file_name,0,$len+1);                        //获取提交目录所在磁盘
Copy after login

The above is the detailed content of How to get the size of disk partition through php file system function. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template