Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 创建一个while循环来打开目录并读取其中的信息_Php - Fatal编程技术网

Php 创建一个while循环来打开目录并读取其中的信息

Php 创建一个while循环来打开目录并读取其中的信息,php,Php,我如何创建一个while循环来打开一个目录,读取不同的目录并获取目录中的图像 我有这个,但不起作用 $TPL['directory']=array() 这可能会奏效。但是我还没有测试代码 // location of directory $directoryPath = 'DIRECTORY_PATH'; // Listing Folders $foldersPath = array_diff(scandir($directoryPath), array('..', '.')); // R

我如何创建一个while循环来打开一个目录,读取不同的目录并获取目录中的图像

我有这个,但不起作用

$TPL['directory']=array()


这可能会奏效。但是我还没有测试代码

// location of directory
$directoryPath = 'DIRECTORY_PATH';

// Listing Folders

$foldersPath = array_diff(scandir($directoryPath), array('..', '.'));

// Reading the files of each folder

foreach($foldersPath as $folderPath){
    $folderPath = $directoryPath.$folderPath."\\";

    // check if the directory exist
    if(is_dir($folderPath)){
        if($folder = opendir($folderPath)){
            while(($fileName = readdir($folder)) !== false){
                if($fileName === '.' || $fileName === '..'){
                    continue;
                }

                $filePath = $folderPath.$fileName;

                // Reading the contents from the file
                $content = file_get_contents($filePath);


            }
        }

    }   
} 

成为严格正确但毫无帮助的注释的承载者:使用code.while循环无法打开目录。如果需要在目录中查找某些文件,有很多方法,一种是使用
glob()
,另一种是使用symfony/finder包,还有一种是递归使用
scandir()
。此外,您可以尝试DirectoryIterator…我很确定,有更多的方法可以做到这一点。您使用的是哪个框架?感谢您添加代码。具体出了什么问题?你有错误吗?
// location of directory
$directoryPath = 'DIRECTORY_PATH';

// Listing Folders

$foldersPath = array_diff(scandir($directoryPath), array('..', '.'));

// Reading the files of each folder

foreach($foldersPath as $folderPath){
    $folderPath = $directoryPath.$folderPath."\\";

    // check if the directory exist
    if(is_dir($folderPath)){
        if($folder = opendir($folderPath)){
            while(($fileName = readdir($folder)) !== false){
                if($fileName === '.' || $fileName === '..'){
                    continue;
                }

                $filePath = $folderPath.$fileName;

                // Reading the contents from the file
                $content = file_get_contents($filePath);


            }
        }

    }   
}