2014-05-14 4 views
-1

나는 그것을 얻을 수있는 디렉토리 크기를 얻을 필요가있는 스토리지 시스템을 구축하고 있습니까? 내가이PHP에서 디렉토리 크기를 얻는 방법

$file_directory = './directory/path'; 
$output = exec('du -sk ' . $file_directory); 
$filesize = trim(str_replace($file_directory, '', $output)) * 1024;$file_directory = './directory/path'; 
$output = exec('du -sk ' . $file_directory); 
$filesize = trim(str_replace($file_directory, '', $output)) * 1024; 

하지만 저를 포기하지 적절한 응답을 사용합니다. 내가 모든 사용자에게 10GB의 저장

+0

에 대한 작품을 읽어주고 싶어이 일 http://www.php.net/manual/en/function.filesize.php – wild

+0

안녕하세요, 이미저기서 보셨나요? (대신에 RecusiveDirectoryIterator를 사용하는 du 변형에 대한 대안이 있음) http://stackoverflow.com/questions/9163456/php-get-the-size-of-a-directory – Thomas

답변

0

사용이 당신

<?php 
    $units = explode(' ', 'B KB MB GB TB PB'); 
    $SIZE_LIMIT = 5368709120; // 5 GB change it to 10 Gb 
    $disk_used = foldersize("/folder/"); 

    $disk_remaining = $SIZE_LIMIT - $disk_used; 

    echo("<html><body>"); 
    echo('diskspace used: ' . format_size($disk_used) . '<br>'); 
    echo('diskspace left: ' . format_size($disk_remaining) . '<br><hr>'); 
    echo("</body></html>"); 


function foldersize($path) { 
    $total_size = 0; 
    $files = scandir($path); 
    $cleanPath = rtrim($path, '/'). '/'; 

    foreach($files as $t) { 
     if ($t<>"." && $t<>"..") { 
      $currentFile = $cleanPath . $t; 
      if (is_dir($currentFile)) { 
       $size = foldersize($currentFile); 
       $total_size += $size; 
      } 
      else { 
       $size = filesize($currentFile); 
       $total_size += $size; 
      } 
     } 
    } 

    return $total_size; 
} 


function format_size($size) { 
    global $units; 

    $mod = 1024; 

    for ($i = 0; $size > $mod; $i++) { 
     $size /= $mod; 
    } 

    $endIndex = strpos($size, ".")+3; 

    return substr($size, 0, $endIndex).' '.$units[$i]; 
} 

?> 
+1

또는 전체 질문과 모든 답변을 확인하십시오. 여기 http://stackoverflow.com/questions/478121/php-get-directory-size –

관련 문제