Copy link to clipboard
Copied
I thought I had cracked this nut from a previous discussion.
http://forums.adobe.com/message/4741283#4741283
DirectoryIterator works great on my local testing server. But on the remote site, results are an unsorted mess.
I found a script that takes care of that pretty nicely.
<?php
class SortingIterator implements IteratorAggregate
{
private $iterator = null;
public function __construct(Traversable $iterator, $callback)
{
if (!is_callable($callback)) {
throw new InvalidArgumentException('Given callback is not callable!');
}
$array = iterator_to_array($iterator);
usort($array, $callback);
$this->iterator = new ArrayIterator($array);
}
public function getIterator()
{
return $this->iterator;
}
}
?>
<!--results-->
<ul>
<?php
function mysort($a, $b)
{
return $a->getPathname() > $b->getPathname();
}
$it = new SortingIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator('PATH/MY_DIRECTORY')), 'mysort');
foreach ($it as $file) {
echo '<li><a href="' . $file . '">' . $file->getFilename() . '</a> - ' . $file->getSize() .' bytes </li>';
}
?>
</ul>
All I need now is a simple way to convert getSize results from bytes to MB.
Any ideas?
Thanks,
Nancy O.
use the php round() function
http://www.php.net/manual/en/function.round.php
round (file->getSize() / 1048576, 2)
Copy link to clipboard
Copied
Divide by 1048576 ??
Copy link to clipboard
Copied
Right.
'. file->getSize() / 1048576 .'
displays a value like this: 7.0703144073486 MB
I need something like this: 7.08 MB or this: 7 MB.
Not sure how to do that.
Nancy O.
Copy link to clipboard
Copied
use the php round() function
http://www.php.net/manual/en/function.round.php
round (file->getSize() / 1048576, 2)
Copy link to clipboard
Copied
Perfect! Thank you, bregent.
Nancy O.
Copy link to clipboard
Copied
You're welcome Nancy.
--bob