views:

33

answers:

2

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.

+4  A: 

take a look at RecursiveDirectoryIterator combine it with strstr or preg_match

maggie
If the filename is known, there is no need for `strstr` or `preg_match`
Gordon
yes your right if the filename (including extension) is known its only comparing strings (after `basename` on `RecursiveDirectoryIterator::getSubPathName()`)
maggie
A: 

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

$filenames = glob('user_data\'. $username . '\*.jpg');
foreach ($filenames as $filename) {
    echo $filename ."\n";
}
Mikey1980
http://php.net/manual/en/function.glob.php
Mikey1980
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