phpglobscandirreaddiropendir

How to search file in folder by using php?


I have a folder called allfiles, and there are some files in this folder, such as

 1212-how-to-sddk-thosd.html
 3454-go-to-dlkkl-sdf.html
 0987-sfda-asf-fdf-12331.html
 4789-how-to-fdaaf-65536.html

I use scandir to list all files, and now I need to find the file by with keywords, example to-dlkkl is the keyword, and I will get the file 3454-go-to-dlkkl-sdf.html.

Glob seems not work, and opendir and readdir are not work well, any ideas?


Solution

  • Use loop foreach and strpos function:

    $files = scandir('allfiles');
    foreach ($files as $file) {
        if (strpos('to-dlkkl', $file) !== false) {
             //file found
        }
    }