0
我有这段代码来获取localhots的根目录和文件,我想用PHP将结果保存在mysql数据库中。我有下面的代码,但它没有正确地执行与数据库的连接:这个想法是在DB中获得这些信息,然后将它加载到表中并发表评论。我感谢任何帮助。使用php和mysql获取和存储根目录和文件
<?php
$pathLen = 0;
function prePad($level)
{
$ss = "";
for ($ii = 0; $ii < $level; $ii++)
{
$ss = $ss . "| ";
}
return $ss;
}
function myScanDir($dir, $level, $rootLen)
{
global $pathLen;
if ($handle = opendir($dir)) {
$allFiles = array();
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
if (is_dir($dir . "/" . $entry))
{
$allFiles[] = "D: " . $dir . "/" . $entry;
}
else
{
$allFiles[] = "F: " . $dir . "/" . $entry;
}
}
}
closedir($handle);
natsort($allFiles);
foreach($allFiles as $value)
{
$displayName = substr($value, $rootLen + 4);
$fileName = substr($value, 3);
$linkName = str_replace(" ", "%20", substr($value, $pathLen + 3));
if (is_dir($fileName)) {
echo prePad($level) . $linkName . "<br>\n";
myScanDir($fileName, $level + 1, strlen($fileName));
} else {
echo prePad($level) . "<a href=\"" . $linkName . "\" style=\"text-decoration:none;\">" . $displayName . "</a><br>\n";
}
}
}
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Site MaP</title>
</head>
<body>
<h1>Archivos y carpetas</h1>
<p style="font-family:'Courier New', Courier, monospace; font-size:small;">
<?php
$root = '../www';
$pathLen = strlen($root);
myScanDir($root, 0, strlen($root));
$sql = "INSERT INTO roots(displayName,fileName,linkName)
VALUES (.'$displayName'., '.$fileName.', '.$linkName.')";
?>
</p>
</body>
</html>)
谢谢你的建议:) –