• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

convert bytes to MB with PHP getSize

Community Expert ,
Oct 03, 2012 Oct 03, 2012

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.

Nancy O'Shea— Product User, Community Expert & Moderator
TOPICS
Server side applications

Views

1.7K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Oct 04, 2012 Oct 04, 2012

use the php round() function

http://www.php.net/manual/en/function.round.php

round (file->getSize() / 1048576, 2)

Votes

Translate

Translate
LEGEND ,
Oct 03, 2012 Oct 03, 2012

Copy link to clipboard

Copied

Divide by 1048576 ??

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 04, 2012 Oct 04, 2012

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.

Nancy O'Shea— Product User, Community Expert & Moderator

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 04, 2012 Oct 04, 2012

Copy link to clipboard

Copied

use the php round() function

http://www.php.net/manual/en/function.round.php

round (file->getSize() / 1048576, 2)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 04, 2012 Oct 04, 2012

Copy link to clipboard

Copied

Perfect!  Thank you, bregent.

Nancy O.

Nancy O'Shea— Product User, Community Expert & Moderator

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 05, 2012 Oct 05, 2012

Copy link to clipboard

Copied

LATEST

You're welcome Nancy.

--bob

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines