PHP - Code to traverse a directory and get all the files(images)

前端 未结 10 1804
被撕碎了的回忆
被撕碎了的回忆 2021-01-27 13:00

i want to write a page that will traverse a specified directory.... and get all the files in that directory...

in my case the directory will only contain images and dis

10条回答
  •  北恋
    北恋 (楼主)
    2021-01-27 13:17

    I use something along the lines of:

    if ($dir = dir('images'))
    {       
        while(false !== ($file = $dir->read()))
        {
            if (!is_dir($file) && $file !== '.' && $file !== '..' && (substr($file, -3) === 'jpg' || substr($file, -3) === 'png' || substr($file, -3) === 'gif'))
            {
                // do stuff with the images
            }
        }
    }
    else { echo "Could not open directory"; }
    

提交回复
热议问题