Q
Q
Qwerty_ABC2021-07-07 17:07:00
PHP
Qwerty_ABC, 2021-07-07 17:07:00

Writes the directory size 2 times to the JSON recursion. What to do?

There is this script:

<?php
function getFilesSize($path)
{
    $fileSize = 0;
    $dirr = scandir($path);

    foreach($dirr as $file)
    {
        if (($file!='.') && ($file!='..'))
            if(is_dir($path . '/' . $file))
                $fileSize += getFilesSize($path.'/'.$file);
            else
                $fileSize += filesize($path . '/' . $file);
    }
    return $fileSize;
}

function tree($dirName) {
  $tree = [];
  $dir = opendir($dirName);
  while (($entry = readdir($dir)) !== false) {
    if ($entry === '.' || $entry === '..') {
      continue;
    }
    $fullName = "{$dirName}\\{$entry}";
    if (is_dir($fullName)) {
      $tree[] = [
    'type' => 'dir',
        'name' => $entry,
    'size' => getFilesSize($dirName),
        'data' => tree($fullName)
      ];
    } else {
      $tree[] = [
    'type' => 'file',
        'name' => $entry,
    'data_change' => time(),
    'size' => filesize($fullName),
    'hash' => md5_file($fullName)
      ];
    }
  }
  return $tree;
}

file_put_contents("update.json", (json_encode(tree('C:\MY_PROJECT'), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)));
?>

It writes a JSON file recursing directories with their size:
60e5b45114a2e716149097.png
60e5b4ed15bcc672260374.png
But recording the size does it for some reason 2 times or more...
The first directory has the correct total size, and the next one has the same and the same size can be repeated a few more once...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question