用PHP迭代子目录
初学者PHP编码器。我编写了这段代码来遍历目录并捕获一些数据,然后使用这些数据更新数据库用PHP迭代子目录,php,Php,初学者PHP编码器。我编写了这段代码来遍历目录并捕获一些数据,然后使用这些数据更新数据库 <?php $servername = "localhost"; $username = "popcorn"; $password = "**********"; $dbname = "professional_test123"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname);
<?php
$servername = "localhost";
$username = "popcorn";
$password = "**********";
$dbname = "professional_test123";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$path = "/home/professional/www/dan/myFiles";
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ('.' === $file) continue;
if ('..' === $file) continue;
$title = pathinfo($file,PATHINFO_BASENAME);
$size = filesize($file);
$myFile[$title]['size'] = filesize($file);
$sql = "UPDATE Files SET Size = '$size' WHERE Name = '$title'";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
closedir($handle);
$conn->close();
这里有一个开始-一个递归函数,如果遇到文件夹,它会调用自己;如果遇到文件,它会运行db查询$myFile是通过引用传入的,因此允许函数动态地向其添加新项,而无需从函数中返回值
$myFile = array();
$path = "/home/professional/www/dan/myFiles";
updateSize($path, $myFile);
function updateSize($path, &$myFile){
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ('.' === $file) continue;
if ('..' === $file) continue;
$full_path = $path.'/'.$file;
if(is_dir($full_path)){
//function calls itself
updateSize($full_path, $myFile);
} else {
$title = pathinfo($file, PATHINFO_BASENAME);
$size = filesize($full_path);
$myFile[$title]['size'] = $size;
$sql = "UPDATE Files SET Size = '$size' WHERE Name = '$title'";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}//while
closedir($handle);
}//if
}
$myFile=array();
$path=“/home/professional/www/dan/myFiles”;
updateSize($path,$myFile);
函数updateSize($path,&$myFile){
如果($handle=opendir($path)){
while(false!=($file=readdir($handle))){
如果('.'=$file)继续;
如果('..'==$file)继续;
$full_path=$path.'/'.$file;
if(is_dir($full_path)){
//函数调用自身
updateSize($full_path,$myFile);
}否则{
$title=pathinfo($file,pathinfo\u BASENAME);
$size=filesize($full\u路径);
$myFile[$title]['size']=$size;
$sql=“更新文件集大小='$Size',其中Name='$title';
if($conn->query($sql)==TRUE){
echo“记录更新成功”;
}否则{
echo“Error:”.$sql.“
”$conn->Error;
}
}
}//当
closedir($handle);
}//如果
}
这是一个开始-一个递归函数,如果遇到文件夹,它会调用自己;如果遇到文件,它会运行db查询$myFile是通过引用传入的,因此允许函数动态地向其添加新项,而无需从函数中返回值
$myFile = array();
$path = "/home/professional/www/dan/myFiles";
updateSize($path, $myFile);
function updateSize($path, &$myFile){
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ('.' === $file) continue;
if ('..' === $file) continue;
$full_path = $path.'/'.$file;
if(is_dir($full_path)){
//function calls itself
updateSize($full_path, $myFile);
} else {
$title = pathinfo($file, PATHINFO_BASENAME);
$size = filesize($full_path);
$myFile[$title]['size'] = $size;
$sql = "UPDATE Files SET Size = '$size' WHERE Name = '$title'";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}//while
closedir($handle);
}//if
}
$myFile=array();
$path=“/home/professional/www/dan/myFiles”;
updateSize($path,$myFile);
函数updateSize($path,&$myFile){
如果($handle=opendir($path)){
while(false!=($file=readdir($handle))){
如果('.'=$file)继续;
如果('..'==$file)继续;
$full_path=$path.'/'.$file;
if(is_dir($full_path)){
//函数调用自身
updateSize($full_path,$myFile);
}否则{
$title=pathinfo($file,pathinfo\u BASENAME);
$size=filesize($full\u路径);
$myFile[$title]['size']=$size;
$sql=“更新文件集大小='$Size',其中Name='$title';
if($conn->query($sql)==TRUE){
echo“记录更新成功”;
}否则{
echo“Error:”.$sql.“
”$conn->Error;
}
}
}//当
closedir($handle);
}//如果
}
“为了理解递归,必须首先理解递归”。您可以将RecursiveDirectoryIterator与RecursiveIteratoryIterator一起使用,也可以将代码封装到递归函数中。我的答案对您有用吗?@IarsAnders是的,谢谢!!!“为了理解递归,你必须首先理解递归”。您可以将RecursiveDirectoryIterator与RecursiveIteratoryIterator一起使用,也可以将代码封装到递归函数中。我的答案对您有用吗?@IarsAnders是的,谢谢!!!我使用了您的if(is_dir($full_path))方法。我不得不改变一些其他的东西,但它现在起作用了,谢谢!我使用了您的if(is_dir($full_path))方法。我不得不改变一些其他的东西,但它现在起作用了,谢谢!