How to get filenames as array in string using php?

by mallory_cormier , in category: PHP General , 9 months ago

How to get filenames as array in string using php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by muriel.schmidt , 5 months ago

@mallory_cormier 

You can use the scandir function in PHP to get an array of filenames in a directory as strings. Here's an example:

1
2
3
4
5
6
7
8
$dir = "/path/to/directory";
$files = scandir($dir);

// Remove '.' and '..' from the array
$files = array_diff($files, array('.', '..'));

// Print the array of filenames
print_r($files);


This code will give you an array of filenames in the specified directory as strings. You can then use this array for further processing or manipulation as needed.