PHP磁盘总空间

发布于 2024-07-12 09:42:35 字数 502 浏览 4 评论 0原文

我需要有关 disk_total_space 函数的帮助..

我的代码中有这个

<?php
          $sql="select * from users order by id";
          $result=mysql_query($sql);
          while($row=mysql_fetch_array($result)) {
?>
Name : <?php echo $row['name']; ?>
Email : <?php echo $row['email']; ?>
Diskspace Available : <?php 
$dir = "C:/xampp/htdocs/freehosting/".$row['name'];
disk_total_space($dir);
} ?>

但是这会为每个用户返回相同的磁盘空间..

任何人都可以告诉我一些信息吗?

谢谢 :)

i need help with disk_total_space function..

i have this on my code

<?php
          $sql="select * from users order by id";
          $result=mysql_query($sql);
          while($row=mysql_fetch_array($result)) {
?>
Name : <?php echo $row['name']; ?>
Email : <?php echo $row['email']; ?>
Diskspace Available : <?php 
$dir = "C:/xampp/htdocs/freehosting/".$row['name'];
disk_total_space($dir);
} ?>

However this return me same disk space for every users ..

Anyone can shed me some light?

thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

伪装你 2024-07-19 09:42:35

https://www.php.net/disk_total_space 说,

“给定一个包含目录的字符串,此函数将返回相应文件系统磁盘分区上的总字节数。”

您可能会看到 C 的 Total_space:

两者都存在替代解决方案Windows 和 Linux

https://www.php.net/disk_total_space says,

"Given a string containing a directory, this function will return the total number of bytes on the corresponding filesystem or disk partition."

You're likely seeing the total_space of C:

Alternative solutions do exist for both Windows and Linux.

人生百味 2024-07-19 09:42:35

我认为你想要的是这样的:

function foldersize($path) {
    $total_size = 0;
    $files = scandir($path);

    foreach($files as $t) {
        if (is_dir(rtrim($path, '/') . '/' . $t)) {
            if ($t<>"." && $t<>"..") {
                $size = foldersize(rtrim($path, '/') . '/' . $t);
                $total_size += $size;
            }
        } else {
            $size = filesize(rtrim($path, '/') . '/' . $t);
            $total_size += $size;
        }   
    }
    return $total_size;
}

function format_size($size) {
    $mod = 1024;

    $units = explode(' ','B KB MB GB TB PB');
    for ($i = 0; $size > $mod; $i++) {
        $size /= $mod;
    }

    return round($size, 2) . ' ' . $units[$i];
}

$SIZE_LIMIT = 5368709120; // 5 GB
$sql="select * from users order by id";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)) {
    $disk_used = foldersize("C:/xampp/htdocs/freehosting/".$row['name']);
    $disk_remaining = $SIZE_LIMIT - $disk_used;
    print 'Name: ' . $row['name'] . '<br>';
    print 'diskspace used: ' . format_size($disk_used) . '<br>';
    print 'diskspace left: ' . format_size($disk_remaining) . '<br><hr>';
}

编辑:递归函数最初有一个错误。 现在它还应该读取原始文件夹中的文件夹以及这些文件夹中的文件夹,依此类推。

I think what you want is something like this:

function foldersize($path) {
    $total_size = 0;
    $files = scandir($path);

    foreach($files as $t) {
        if (is_dir(rtrim($path, '/') . '/' . $t)) {
            if ($t<>"." && $t<>"..") {
                $size = foldersize(rtrim($path, '/') . '/' . $t);
                $total_size += $size;
            }
        } else {
            $size = filesize(rtrim($path, '/') . '/' . $t);
            $total_size += $size;
        }   
    }
    return $total_size;
}

function format_size($size) {
    $mod = 1024;

    $units = explode(' ','B KB MB GB TB PB');
    for ($i = 0; $size > $mod; $i++) {
        $size /= $mod;
    }

    return round($size, 2) . ' ' . $units[$i];
}

$SIZE_LIMIT = 5368709120; // 5 GB
$sql="select * from users order by id";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)) {
    $disk_used = foldersize("C:/xampp/htdocs/freehosting/".$row['name']);
    $disk_remaining = $SIZE_LIMIT - $disk_used;
    print 'Name: ' . $row['name'] . '<br>';
    print 'diskspace used: ' . format_size($disk_used) . '<br>';
    print 'diskspace left: ' . format_size($disk_remaining) . '<br><hr>';
}

edit: the recursive function had a bug in it originally. Now it should also read folders inside the original folders, and folders inside those folders, and so on.

給妳壹絲溫柔 2024-07-19 09:42:35

我认为该方法只会告诉您有关给定目录所在的文件系统或分区的信息。 您可以尝试简单地使用 du

$space_used=`du -sh $dir`;

-s 总结整个目录, -h 以“人类”单位(例如 MB 和 GB)返回结果。

编辑:抱歉,我错过了这是在 WIndows 上进行的。 我会留下答案,以防它帮助搜索类似问题的人。 对于 Windows,尝试 PHP 手册中的此建议

I think that method will only tell you information about the filesystem or partition the given directory is on. You could try simply shelling out to du though:

$space_used=`du -sh $dir`;

-s summarizes the entire dir, -h returns the result in "human" units like MB and GB.

Edit: apologies, I missed that this was on WIndows. WIll leave the answer in case it helps someone searching for a similar problem. For windows, try this suggestion in the PHP manual

独守阴晴ぅ圆缺 2024-07-19 09:42:35

PHP.net 上该函数的注释似乎表明,这给出了 $dir 所在的驱动器/分区的空间,而不是 $dir 本身的大小。

The comments on the function on PHP.net appear to indicate that this gives the space of the drive/partition $dir is in, not the size of $dir itself.

再浓的妆也掩不了殇 2024-07-19 09:42:35

根据 disk_total_space() 的文档,返回的值是目录所在的文件系统。 它不计算目录及其子目录中使用的空间。

您可以使用 du 或寻求更便携的解决方案:

$total = 0;

foreach (new RecursiveDirectoryIterator($dir) as $entry)
{
    if ($entry->isFile())
        $total += $entry->getSize();
}

According to the docs for disk_total_space(), the value returned is for the filesystem the directory is located on. It doesn't count the space used in a directory + its subdirectories.

You could shell out to du or for a more portable solution:

$total = 0;

foreach (new RecursiveDirectoryIterator($dir) as $entry)
{
    if ($entry->isFile())
        $total += $entry->getSize();
}
掐死时间 2024-07-19 09:42:35

您可以使用该功能:

function size_directory($ruta)
{
    $gestor = opendir($ruta);
        $total = 0;
        while (($archivo = readdir($gestor)) !== false) {
            $ruta_completa = $ruta . "/" . $archivo;
            if ($archivo != "." && $archivo != "..") {
            $total += filesize($ruta_completa);
            }
        }
    return round($total / 1024, 0);
}

You could use the function:

function size_directory($ruta)
{
    $gestor = opendir($ruta);
        $total = 0;
        while (($archivo = readdir($gestor)) !== false) {
            $ruta_completa = $ruta . "/" . $archivo;
            if ($archivo != "." && $archivo != "..") {
            $total += filesize($ruta_completa);
            }
        }
    return round($total / 1024, 0);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文