我正在使用 CLI php 脚本编写构建/部署脚本。
假设我有一个目录/environment,其中只有两个损坏的符号链接。
我在跑glob(/environment/{,.}*)。当我在 glob 上 foreach 时,我看到的只是.和..。符号链接永远不会出现在列表中。
如何unlink()使用 PHP遍历目录、检测损坏的符号链接以及它们?
在损坏的符号链接上is_link()返回true并file_exists()返回false。
由于glob()没有列出损坏的符号链接,您必须以不同的方式列出内容。这是一个使用示例scandir()
foreach(scandir($dir) as $entry) {
$path = $dir . DIRECTORY_SEPARATOR . $entry;
if (is_link($path) && !file_exists($path)) {
@unlink($path);
}
}
Run Code Online (Sandbox Code Playgroud)