如何使用 php 检测目录或目录中文件的更改
How to detect changes in directories or file inside that directory using php
我有这样的目录结构:
[Products] => Array
(
[Category1] => Array
(
[product1] => Array
(
[documents] => Array
(
[0] => Attachment1.pdf
[1] => Attachment2.pdf
)
)
)
)
如果产品目录中的任何目录或文件有任何变化
喜欢:
- 产品 1 更改为产品 2
- 添加了新目录
- 文件中的任何更改 Attachment.pdf
- 新文档已添加到目录。
到目前为止我尝试过的:
$dir="PATH TO PRODUCT DIRECTORY"
$stats = stat($dir);
但 $stats['mtime'] 仅检测到产品 children 的变化(比如将 Category1 更改为 Category2 或向产品添加新目录)但未检测到全部 children 产品(比如我将 product1 更改为 product2)等等。
谁能帮我删除 children 产品中的任何更改。
这是我已经实施的解决方案。通过使用这些函数,我可以获得一个 MD5 哈希码,并且我可以使用这个哈希码来检查目录结构或任何文件中是否有任何更改。
$dir="PATH TO PRODUCT DIRECTORY";
public function directoryHash($dir, $item_name){
$dirStr=$this->directoryStruture($dir, $separator= DIRECTORY_SEPARATOR, $root='');
$strHash=md5(json_encode($dirStr));
return $strHash;
}
public function directoryStruture($dir, $separator = DIRECTORY_SEPARATOR, $root = '') {
$result = array();
$cdir = scandir($dir);
$result = array();
if ($root === '') {
$root = $dir;
}
$cdir = scandir($dir);
foreach ($cdir as $key => $value) {
if (!in_array($value, array(".", ".."))) {
$current = $dir . $separator . $value;
if (is_dir($current)) {
$result['items'][] = $value;
$result[$value] = $this->directoryStruture($current, $separator, $root);
} else {
$filePath = str_replace($root, '', $current);
$result[] = $filePath.filemtime($current);
}
}
}
return $result;
}
如果有更好的办法。请告诉我。
谢谢,
男.
我有这样的目录结构:
[Products] => Array
(
[Category1] => Array
(
[product1] => Array
(
[documents] => Array
(
[0] => Attachment1.pdf
[1] => Attachment2.pdf
)
)
)
)
如果产品目录中的任何目录或文件有任何变化 喜欢:
- 产品 1 更改为产品 2
- 添加了新目录
- 文件中的任何更改 Attachment.pdf
- 新文档已添加到目录。
到目前为止我尝试过的:
$dir="PATH TO PRODUCT DIRECTORY"
$stats = stat($dir);
但 $stats['mtime'] 仅检测到产品 children 的变化(比如将 Category1 更改为 Category2 或向产品添加新目录)但未检测到全部 children 产品(比如我将 product1 更改为 product2)等等。
谁能帮我删除 children 产品中的任何更改。
这是我已经实施的解决方案。通过使用这些函数,我可以获得一个 MD5 哈希码,并且我可以使用这个哈希码来检查目录结构或任何文件中是否有任何更改。
$dir="PATH TO PRODUCT DIRECTORY";
public function directoryHash($dir, $item_name){
$dirStr=$this->directoryStruture($dir, $separator= DIRECTORY_SEPARATOR, $root='');
$strHash=md5(json_encode($dirStr));
return $strHash;
}
public function directoryStruture($dir, $separator = DIRECTORY_SEPARATOR, $root = '') {
$result = array();
$cdir = scandir($dir);
$result = array();
if ($root === '') {
$root = $dir;
}
$cdir = scandir($dir);
foreach ($cdir as $key => $value) {
if (!in_array($value, array(".", ".."))) {
$current = $dir . $separator . $value;
if (is_dir($current)) {
$result['items'][] = $value;
$result[$value] = $this->directoryStruture($current, $separator, $root);
} else {
$filePath = str_replace($root, '', $current);
$result[] = $filePath.filemtime($current);
}
}
}
return $result;
}
如果有更好的办法。请告诉我。
谢谢,
男.