How to get filenames as array in string using php?

by mallory_cormier , in category: PHP General , a year ago

How to get filenames as array in string using php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by muriel.schmidt , a year 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.

Related Threads:

How to split a string into an array in PHP?
How to convert a json string to an array in php?
How to join array elements into a string in PHP?
How to parse a xml string to array in php?
How to store the string values into an array in php?
How to find filenames with a specific extension using regex?