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>)
謝謝你的建議:) –