IMO edubem's answer là con đường để đi, tuy nhiên bạn có thể làm một cái gì đó như thế này:
if (sha1(serialize(Map('/path/to/directory/', true))) != /* previous stored hash */)
{
// directory contents has changed
}
Hoặc một yếu hơn/phiên bản nhanh hơn:
if (Size('/path/to/directory/', true) != /* previous stored size */)
{
// directory contents has changed
}
Sau đây là các chức năng sử dụng:
function Map($path, $recursive = false)
{
$result = array();
if (is_dir($path) === true)
{
$path = Path($path);
$files = array_diff(scandir($path), array('.', '..'));
foreach ($files as $file)
{
if (is_dir($path . $file) === true)
{
$result[$file] = ($recursive === true) ? Map($path . $file, $recursive) : $this->Size($path . $file, true);
}
else if (is_file($path . $file) === true)
{
$result[$file] = Size($path . $file);
}
}
}
else if (is_file($path) === true)
{
$result[basename($path)] = Size($path);
}
return $result;
}
function Size($path, $recursive = true)
{
$result = 0;
if (is_dir($path) === true)
{
$path = Path($path);
$files = array_diff(scandir($path), array('.', '..'));
foreach ($files as $file)
{
if (is_dir($path . $file) === true)
{
$result += ($recursive === true) ? Size($path . $file, $recursive) : 0;
}
else if (is_file() === true)
{
$result += sprintf('%u', filesize($path . $file));
}
}
}
else if (is_file($path) === true)
{
$result += sprintf('%u', filesize($path));
}
return $result;
}
function Path($path)
{
if (file_exists($path) === true)
{
$path = rtrim(str_replace('\\', '/', realpath($path)), '/');
if (is_dir($path) === true)
{
$path .= '/';
}
return $path;
}
return false;
}
Nguồn
2009-12-13 10:13:48
Cảm ơn câu trả lời nhưng hầu hết câu trả lời của bạn không hợp lệ cho tập lệnh mà nhiều người dùng cuối trên nhiều hệ thống khác nhau sẽ sử dụng. Để hiểu rõ hơn về những gì tôi đang thực hiện điều này, hãy xem trang thư viện tại http://code.web-geek.net/ck-gallery – PHLAK