How to traverse folders to get the image directory name and file name through php

不言
Release: 2023-04-03 06:00:01
Original
4057 people have browsed it

This article mainly introduces how to traverse folders to obtain image directory names and file names through php. It has certain reference value. Now I share it with everyone. Friends in need can refer to the

code As follows:

public function doImportImages(Q_Request $input, Q_Response $output)
{
    $dir    = VAR_PATH . 'images';      //此处为存放所有图片的目录
    $result = $this->_importImages($dir);    //获得的所有图片目录以及文件名,以多维数组形式返回,方便处理
}private function _importImages($dir)
{
    $handle = opendir($dir); //读资源
    if (!$handle) {
        return false;
    }
    $result = [];
    $module = 'images';

    while (($file = readdir($handle)) !== false) {
        if ($file != '.' && $file != '..') {
            $curPath = $dir . '/' . $file;

            if (is_dir($curPath)) {//判断是否为目录,递归读取文件
                $result[$file] = $this->_importImages($curPath);
            } else {
                $uploadObj = Lib_Upload::getUploadObj($module, false);
                $uploadObj->doUpload($curPath, Q_Upload::UPDATE_TYPE_PATH);   //此处做了图片上传动作,如果不需要请自行调整
                $data     = $uploadObj->data();
                $result[] = $data['fileCreateUrl'];
            }
        }
    }
    closedir($handle);

    return $result;
}
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to solve the array reference problem left by PHP after the foreach loop

About how to call PHP Problems shared by phantomjs to WeChat applet

The above is the detailed content of How to traverse folders to get the image directory name and file name through php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template