How to get the file names inside a directory using PHP?
I couldn’t find the relevant command using Google, so I hope that this question will help those who are asking along the similar lines.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There’s a lot of ways. The older way is
scandirbutDirectoryIteratoris probably the best way.There’s also
readdir(to be used withopendir) andglob.Here are some examples on how to use each one to print all the files in the current directory:
DirectoryIteratorusage: (recommended)scandirusage:opendirandreaddirusage:globusage:As mentioned in the comments,
globis nice because the asterisk I used there can actually be used to do matches on the files, soglob('*.txt')would get you all the text files in the folder andglob('image_*')would get you all files that start with image_