简体   繁体   English

使用php和mysql获取并存储根目录和文件

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

I have this code to get the root directory and files of a localhots, I want to save the results in a mysql database with PHP. 我有这段代码来获取根目录和localhots的文件,我想将结果保存在PHP中的mysql数据库中。 I have the following code but it does not correctly perform the connection with the databases: The idea is to have this information in DB and later load it into a table and make comments. 我有以下代码,但是它不能正确执行与数据库的连接:想法是将这些信息存储在DB中,然后再将其加载到表中并进行注释。 I appreciate any help. 感谢您的帮助。

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

The SQL you've written is not actually getting executed, nor is any connection being made to a database to execute it. 您编写的SQL实际上并没有被执行,也没有与数据库建立任何连接来执行它。 It's just being assigned to a variable. 它只是被分配给一个变量。

To conduct database operations in a safe and transparent manner, I strongly recommend using PDO (documentation: http://php.net/manual/en/book.pdo.php ). 为了以安全透明的方式进行数据库操作,我强烈建议使用PDO(文档: http : //php.net/manual/zh/book.pdo.php )。 This is a class that provides methods for things like creating database connections, parameterizing and executing queries. 此类提供了一些方法,例如创建数据库连接,参数化和执行查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM