我从另一个程序员那里得到了一份工作。不幸的是,库中也充满了测试文件,我不知道实际使用了哪些文件。我想通过查看文件中的链接来过滤掉它。
手工要花很长时间。我写了一个代码,但没有列出所有正在使用的文件。
清理根目录就足够了。
谢谢您的建议!
$files = scandir('/public_html/');
$hrefs = array();
foreach ($files as $file) {
$info = pathinfo($file);
if ($info["extension"] == "php") {
$php = file_get_contents($file);
$dom = new DOMDocument();
$dom->loadHTML($php);
$tags = $dom->getElementsByTagName('a');
foreach ($tags as $tag) {
$href = $tag->getAttribute('href');
$href = basename($href);
if (is_file($href) && !in_array($href, $hrefs)) {
$hrefs[] = $href;
}
}
$tags = $dom->getElementsByTagName('form');
foreach ($tags as $tag) {
$href = $tag->getAttribute('action');
$href = basename($href);
if (is_file($href) && !in_array($href, $hrefs)) {
$hrefs[] = $href;
}
}
$tags = $dom->getElementsByTagName('img');
foreach ($tags as $tag) {
$href = $tag->getAttribute('src');
$href = basename($href);
if (is_file($href) && !in_array($href, $hrefs)) {
$hrefs[] = $href;
}
}
}
}
print_r($hrefs, true);
答案 0 :(得分:1)
我只是迅速地将以下内容放在一起,以扫描目录和子目录,以根据文件中发现的内容列出文件-可能有用。
error_reporting( E_ALL );
ini_set( 'display_errors', 1 );
set_time_limit( 60 );
/* edit to suit. Choose directory, file extensions and exclusions */
$config=(object)array(
'directory' => __DIR__,
'extensions' => array( 'php', 'html', 'htm' ),
'exclusions' => array(
'bookmarks_11_01_2019.html',
'bookmarks_05_01_2019.html'
)
);
function getnodes($type,$attr){
/*
helper function to get $type elements
and return attribute $attr
*/
global $dom;
global $info;
global $ext;
$col=$dom->getElementsByTagName( $type );
$tmp=[];
if( $col->length > 0 ){
foreach( $col as $node ){
$tmp[]=array(
$attr => $node->getAttribute( $attr ),
'file' => $info->getFileName(),
'dir' => $info->getPathInfo()->getRealPath(),
'type' => $type,
'ext' => $ext
);
}
}
return $tmp;
}
libxml_use_internal_errors( true );
$dom=new DOMDocument;
$links=[];
/* create the recusive iterators */
$dirItr=new RecursiveDirectoryIterator( $config->directory, RecursiveDirectoryIterator::KEY_AS_PATHNAME );
foreach( new RecursiveIteratorIterator( $dirItr, RecursiveIteratorIterator::CHILD_FIRST ) as $obj => $info ) {
if( $info->isFile() ){
$ext = pathinfo( $info->getFileName(), PATHINFO_EXTENSION );
/* only scan files of specified extensions that are not in the exclusions list */
if( in_array( $ext, $config->extensions ) && !in_array( $info->getFileName(), $config->exclusions ) ){
/* load a new file into DOMDocument */
$dom->loadHTMLFile( $info->getPathName() );
/* ignore errors */
libxml_clear_errors();
/* find elements that may be of interest */
$links=array_merge(
$links,
getnodes( 'a', 'href' ),
getnodes( 'form', 'action' ),
getnodes( 'img', 'src' ),
getnodes( 'iframe', 'src' )
);
}
}
}
/* display scan results*/
printf( '<pre>%s</pre>', print_r( $links, true ) );