php - How to list all files in folders and sub-folders using scandir and display them as an option in select? -


I have a piece of code that I want to list all the files in folders and sub-folders in the current path I would like to modify. I have found many possible solutions I tried to implement them in reality, but nothing seemed to work, so I'm not sure whether they are actually working or it's just wrong Was implemented in Either way this is my current code:

  & lt ;? Php $ currentdir = 'C: / xampp / htdocs / test /'; // Change to your directory $ dir = opendir ($ currentdir); Echo '& lt; Select Name = "Exercise" & gt; '; $ File = preg_grep ('/^([^.])/', Scandinum ($ dir)); While ($ file = readdir ($ dir)) {if ($ file! = "." & Amp; $ file! = "..") {echo "

This code currently shows the result:

  $ currentdir option 1 Option 2 Option 3 subfolder 1 subfolder 2 /  

Someone helps me and to write something like this in order to display files from folders and other sub-folders using the current one to show / write / write / write to me: / P>

  $ currentdir Option1 Option2 Option3 Sub-FOLDER1 Option 1 Option 2 Option 3 Subfolder 2 Option 1 Option 2 Option 3  

I became really desperate for the solution and thank you all beforehand.

While other answers are correct and correct, then allow me to add my own solution:

  function dirToOptions ($ path = __DIR__, $ level = 0) {$ item = scanner ($ path); External currency ($ item as item $) {// Avoid entangled items with a dot (= hidden or nav) if (SRPO ($ item, '.') === 0) {continue; } $ Full path = $ path DIRECTORY_SEPARATOR $ item; // File structure $ item = str_repeat ('& amp; nbsp;', $ level * 3) Add some white space to better mimic. $ Item; // file if (is_file ($ full text)) {resonant "option" gt; $ Item & lt; / Option & gt; ";} // dir else if (is_dir ($ fullPath)) {// immediatly close the optgroup to prevent nested optgroups"  

It uses calls to fetch subtitles. Note that I added some white space before each item to better imitate the file structure. Apart from this, I stopped the optgroup to stop the nested opcupgup from ending with the elements, Which is invalid HTML.


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -