1

Why is the following not working.

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

If i do not explode and i just do echo '<li>'.$file.'</li>'; then i get a string like this per li drwxr-xr-x 10 user 4.0K Sep 8 16:06 _test

I"m trying to get only the size and not the whole string. What am i doing wrong.

3
  • are you splitting your string by double space Commented Oct 7, 2011 at 4:06
  • 2
    You are aware that PHP has filesystem functions, aren't you Commented Oct 7, 2011 at 4:11
  • what result u u getting for now? Commented Oct 7, 2011 at 7:53

3 Answers 3

3

You can also use PHP for that:

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

Which gives you a nice associative array like:

[./curl.php] => 1499
[./db.php] => 10267
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, That works perfectly, but i wanted to know why exec was not working.
1

If you just want to get the sizes of the file,try this:

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

Here's my output:

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

Comments

0

I wouldn't say that you are doing something wrong - your approach is perfectly acceptable. However, if you want to avoid the explode() you could do something like:

$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>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.