php文件操作

读取文件目录

<?php
function readDirctory($path)
{
    if (!is_dir($path)) {
        return false;
    }
    $handle = opendir($path);

    $arr = [];
    while ( ($file = readdir($handle)) !== false ) {

        if ($file != '.' && $file != '..') {
            if (is_file($path . '/' . $file)) {
                $arr['file'][] = $file;
            }
            if (is_dir($path . '/' . $file)) {
                $arr['dir'][] = $file;
            }

        }

    }
    closedir($handle);
    return $arr;

}

$path = "abc";
$res = readDirctory($path);
print_r($res);

 转换字节大小

function transByte($fileSize = 0)
{
    $unit = ['Byte', 'Kb', 'Mb', 'Tb', 'Eb'];
    $i = 0;

    while ( $fileSize >= 1024 ) {
        $fileSize/=1024;
        $i++;
    }

    return round($fileSize,2).$unit[$i];
}

 

posted @ 2021-08-17 01:41  X__cicada  阅读(23)  评论(0编辑  收藏  举报