簡體   English   中英

使用php和mysql獲取並存儲根目錄和文件

[英]Get and storage the root directories and files using php and mysql

我有這段代碼來獲取根目錄和localhots的文件,我想將結果保存在PHP中的mysql數據庫中。 我有以下代碼,但是它不能正確執行與數據庫的連接:想法是將這些信息存儲在DB中,然后再將其加載到表中並進行注釋。 感謝您的幫助。

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM