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.

1

2 Answers 2

4

take a look at RecursiveDirectoryIterator combine it with strstr or preg_match

Sign up to request clarification or add additional context in comments.

2 Comments

If the filename is known, there is no need for strstr or preg_match
yes your right if the filename (including extension) is known its only comparing strings (after basename on RecursiveDirectoryIterator::getSubPathName())
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"; } 

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.