0

i have a situation,i am creating a folder everytime when new user register.the folder name is equal to username.so therefore user_data folder contains all folder which name is equal to username. when user upload something then it directly save to its desired username folder.

so now i want to search perticular file from these folder. i know that, that particular file save in user_data folder but i dont know in user_data foder which folder contain that file. so what will be the code for searching file in directory.

hakre
  • 193,403
  • 52
  • 435
  • 836
Pratik Gujarathi
  • 748
  • 10
  • 22
  • 40
  • 1
    possible duplicate of [PHP SPL RecursiveDirectoryIterator RecursiveIteratorIterator retrieving the full tree](http://stackoverflow.com/questions/2418068/php-spl-recursivedirectoryiterator-recursiveiteratoriterator-retrieving-the-full) – Gordon Aug 31 '10 at 11:46

2 Answers2

4

take a look at RecursiveDirectoryIterator combine it with strstr or preg_match

teemitzitrone
  • 2,250
  • 17
  • 15
  • If the filename is known, there is no need for `strstr` or `preg_match` – Gordon Aug 31 '10 at 11:45
  • yes your right if the filename (including extension) is known its only comparing strings (after `basename` on `RecursiveDirectoryIterator::getSubPathName()`) – teemitzitrone Aug 31 '10 at 11:52
4

glob() should do the trick. Here's an example of its usage. hope it helps:

$filenames = glob('user_data\\'. $username . '\\*.jpg');
foreach ($filenames as $filename) {
    echo $filename ."\n";
}
Community
  • 1
  • 1
Mikey1980
  • 971
  • 4
  • 15
  • 24
  • 1
    But there's an issue when `$username` contains specially treated characters such as `*` and `?`, therefore using the `DirectoryIterator` class and its derivatives is IMO a more suited approach to the problem. – Frxstrem Aug 31 '10 at 14:39