2017-07-14 44 views
0

我有這段代碼來獲取localhots的根目錄和文件,我想用PHP將結果保存在mysql數據庫中。我有下面的代碼,但它沒有正確地執行與數據庫的連接:這個想法是在DB中獲得這些信息,然後將它加載到表中並發表評論。我感謝任何幫助。使用php和mysql獲取和存儲根目錄和文件

<?php 
$pathLen = 0; 

function prePad($level) 
{ 
    $ss = ""; 

    for ($ii = 0; $ii < $level; $ii++) 
    { 
    $ss = $ss . "|&nbsp;&nbsp;"; 
    } 

    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>) 

回答

0

你寫的SQL實際上並沒有被執行,也沒有任何連接到數據庫來執行它。它只是被分配給一個變量。

爲了以安全和透明的方式進行數據庫操作,我強烈建議使用PDO(文檔:http://php.net/manual/en/book.pdo.php)。 這是一個爲創建數據庫連接,參數化和執行查詢等提供方法的類。

+0

謝謝你的建議:) –