开发者

Verify Files in a folder

I want to verify files stored in a directory, so i wrote this script.

<?php
$files = scandir('..');
for ($i=0;$i<count($files);$i++)
{
 $file2 = realpath($files[$i]);
 if (!is_dir($file2))
  $hash = sha1_file($file2);
 else
  $hash = 'Dir';
 echo '<tr><td>'.$i.'</td><td>'.$file2.'</td><td>'.$hash.'</td><td>'.date ("F d Y H:i:s.",filemtime($file2)).'</td></tr>';
 $i;
}
?>

it works nice, but if i include this script from a deeper a directory then it fails.

-www
 |_Sou
   |_Inner
     |_MoreInner

my script is located in inner folder and want to access it from MoreInner folder usin开发者_如何学编程g include but it shows error FILE NOT FOUND/ PERMISSION DENIED

  • What i want to do To get SHA1_FILE of files stored in the just above directory.


Using a RecurisveDirectoryIterator would be the most elegant way:

$it = new RecursiveDirectoryIterator('..', FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS);
foreach (new RecursiveIteratorIterator($it, RecursiveIteratorIterator::SELF_FIRST) as $item) {
    if (!$item->isDir()) {
        $hash = sha1_file($item->getRealPath());
    } else {
        $hash = 'Dir';
    }
    echo $item->getRealPath().' '.$hash.' '.date ("F d Y H:i:s.", $item->getMTime())."\n";
}


As for the code example how to convert your code into a function and add a parameter for the path:

Make this function globally available to any scripts of your application where-ever in which directory you place them for your application:

function verfiyFiles($path) {
 $files = scandir($path);
 for ($i=0;$i<count($files);$i++)
 {
  $file2 = realpath($files[$i]);
  if (!is_dir($file2))
   $hash = sha1_file($file2);
  else
   $hash = 'Dir';
  echo '<tr><td>'.$i.'</td><td>'.$file2.'</td><td>'.$hash.'</td><td>'.date ("F d Y H:i:s.",filemtime($file2)).'</td></tr>';
  $i;
 }
}

Then within any script of your application you can make use of it:

verfiyFiles($path); # where $path is a path to the directory, e.g. '..'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜