2011-10-07 120 views
1

爲什麼下面的不起作用。php Exec爆炸陣列

$directory = './'; 
exec('ls -loh ' . $directory, $directory_list); 
echo '<ul>'; 
foreach ($directory_list as $file) { 
    $x = explode(' ', $file); 
    echo '<li>' . $x[3] . '</li>'; 
} 
echo '</ul>'; 

,如果我不爆炸,我只是做echo '<li>'.$file.'</li>';然後我得到一個字符串,這樣每李 drwxr-xr-x 10 user 4.0K Sep 8 16:06 _test

我「米試圖讓只有大小,而不是整個字符串。我是什麼做錯了

+0

是否將您的字符串拆分爲雙倍空間 – Gowri

+0

我正在使用單個空間。 – Pinkie

+2

你知道PHP有[文件系統功能](http://php.net/manual/en/ref.filesystem.php),是不是你 – NullUserException

回答

1

如果你只是想獲得的文件的大小,試試這個:

exec("ls -sh ./", $results); 
foreach(array_slice($results,1,count($results)) as $file) { 
    echo $file . "\n"; 
} 

這裏是我的輸出:

4.0K 24 
    0 BookingTest.php 
    0 date 
4.0K date.php 
4.0K exec2.php 
4.0K somefile 
4.0K file.php 
4.0K file 
+0

很好,它的工作原理。 – Pinkie

0

我不會說,你正在做的事情錯了 - 你的做法是完全可以接受的。然而,如果你想避免explode(),你可以這樣做:。

$directory = './'; 
exec('ls -loh | awk \'{print $4}\'' . $directory, $directory_list); 
echo '<ul>'; 
foreach ($directory_list as $file_size) { 
    echo '<li>' . $file_size . '</li>'; 
} 
echo '</ul>'; 
3

您也可以使用PHP爲:

$files = glob("./*"); 
$files = array_combine($files, array_map("filesize", $files)); 

這給你一個很好的關聯數組,如:

[./curl.php] => 1499 
[./db.php] => 10267 
+0

謝謝,這很好,但我想知道爲什麼exec不工作。 – Pinkie