简体   繁体   English

使用PHP迭代子目录

[英]Iterating through sub-directories with PHP

Beginner PHP coder. 初学者PHP编码器。 I've written this piece of code to iterate through a directory and capture some data then use that data to update a db. 我编写了这段代码来遍历目录并捕获一些数据然后使用该数据来更新数据库。

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

What I want to do now is iterate through the root directories many sub-directories and collect the same data from all of them. 我现在要做的是遍历根目录中的许多子目录并从所有子目录中收集相同的数据。 I read that I can use RecursiveDirectoryIterator in conjunction with RecursiveIteratorIterator to accomplish this but I'm not sure how to incorporate it into my code. 我读到我可以将RecursiveDirectoryIterator与RecursiveIteratorIterator结合使用来完成此操作,但我不确定如何将它合并到我的代码中。 Many thanks for the help. 非常感谢您的帮助。

Here's a start - a recursive function that calls itself if it encounters a folder, or runs your db query if it encounters a file. 这是一个开始 - 一个递归函数,如果遇到文件夹则调用自身,或者遇到文件时运行db查询。 $myFile is passed in by reference so the function is allowed to add new items to it on the fly, without needing to return a value out of the function. $ 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
}

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

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