3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

scandirの注意点

Last updated at Posted at 2015-05-28

phpのscandirは、結果に...が含まれる。
ディレクトリをコピーした後、正しくコピーされたかをコピー元とコピー先の内容のサイズ比較によって確認する場合、...を除いて比較する必要がある。

function getSizeArray($somepath) {
  $sizeArray = array();

  // ディレクトリ内のファイル一覧を入手。.も..もあるよ
  $files = scandir($somepath);
  
  // キー:ファイル名、値:ファイルサイズ
  foreach ($files as $file) {
    $sizeArray[$file] = filesize($somepath.$file);
  }
}
.
.
$srcDirSizeArray  = getSizeArray($srcDirpath);
$destDirSizeArray = getSizeArray($destDirpath);

// ダメな例
foreach ($destDirSizeArray as $key => $size) {
  if ($srcDirSizeArray[$key] !== $size) {
    return false;
  }
}
.
.

コピー先ディレクトリの親ディレクトリ..がコピー元の親ディレクトリと完全に同じにならないならば、ここのサイズがいずれずれることは必然。
こうやって運用環境でしか起こらないバグが生まれていくのか。。
...を含まないls -A相当のscandirはないのかね。

3
2
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?