解析访问到路径地址,获取相关值,代码如下:
if(!empty($url)){
$url = "http://ip/file/".$url;
// 初始化cURL会话
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$html = curl_exec($ch);
curl_close($ch);
$html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
$dom = new \DOMDocument();
@$dom->loadHTML($html); // 使用@来抑制可能的警告
$xpath = new \DOMXPath($dom);
$files = [];
$rows = $xpath->query('//table/tr[not(th)]'); // 选择不包含表头的行
foreach ($rows as $row) {
$cells = $row->childNodes;
$nameCell = $cells->item(1)->getElementsByTagName('a')->item(0);
$timeCell = $cells->item(2);
$sizeCell = $cells->item(3);
if ($nameCell && $timeCell) {
$files[] = [
'name' => $nameCell->nodeValue,
'last_modified' => trim($timeCell->nodeValue),
'size' => trim($sizeCell->nodeValue)
];
}
}
foreach ($files as $file) {
if (strpos($file['name'], '.') !== false) {
$file['name'] = $url.$file['name'];
$fileArray[] = $file;
}
}
}