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. 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. I appreciate any help.
<?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>)
The SQL you've written is not actually getting executed, nor is any connection being made to a database to execute it. 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 ). This is a class that provides methods for things like creating database connections, parameterizing and executing queries.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.