2014-12-03 1 views
0

특정 디렉토리에서 가장 큰 파일의 절대 경로를 어떻게 반환합니까?PHP 절대 파일

나는 낚시를하고 있었고 콘크리트가 보이지 않았습니까?

glob()과 관련이 있다고 생각합니까?

답변

1
$sz = 0; 
$dir = '/tmp'; // will find largest for `/tmp` 
if ($handle = opendir($dir)) { // will iterate through $dir 
    while (false !== ($entry = readdir($handle))) { 
    if(($curr = filesize($dir . '/' . $entry)) > $sz) { // found larger! 
     $sz = $curr; 
     $name = $entry; 
    } 
    } 
} 
echo $dir . '/' . $name; // largest 
1
$dir = 'DIR_NAME'; 
$max_filesize = 0; 
$path= ''; 

foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator(realpath($dir),FilesystemIterator::SKIP_DOTS)) as $file){ 
    if ($file->getSize() >= $max_filesize){ 
     $max_filesize = $file->getSize(); 
     $path = $file->getRealPath(); // get absolute path    
    } 
} 
echo $path; 
0
function getFileSize($directory) { 
    $files = array(); 
    foreach (glob($directory. '*.*') as $file) { 
     $files[] = array('path' => $file, 'size' => filesize($file)); 
    } 
    return $files; 
} 

function getMaxFile($files) { 
    $maxSize = 0; 
    $maxIndex = -1; 
    for ($i = 0; $i < count($files); $i++) { 
     if ($files[$i]['size'] > $maxSize) { 
      $maxSize = max($maxSize, $files[$i]['size']); 
      $maxIndex = $i; 
     } 
    } 
    return $maxIndex; 
} 

사용 :

$dir = '/some/path'; 
$files = getFileSize($dir); 
echo '<pre>'; 
print_r($files); 
echo '</pre>'; 

$maxIndex = getMaxFile($files); 
var_dump($files[$maxIndex]); 
관련 문제