Auto-populate a list with the filenames within a folder
In this article we will describe how to auto-populate a list with the name of the files in a folder of your choice.
In order to achieve this paste the following code to the "Items" text area. This area accepts custom PHP script.
//<code> // This shouldn't be needed in Joomla 3.x, but better safe than sorry! jimport('joomla.filesystem.folder'); // Initialize our empty array that will hold the values. $items = array(); // If you want to show a "Please select" item ([c] means that the value is selected by default), uncomment the following line: // $items[] = "|Please select[c]"; // Grab all the files from your selected folder. JFolder::files() is a Joomla! framework function. $files = JFolder::files('/path/to/your/folder'); // You can use paths relative to your Joomla! root, which is stored in the constant JPATH_SITE: // $files = JFolder::files(JPATH_SITE.'/language/en-GB'); // The above grabs all frontend language files from the en-GB folder. // Now, let's merge the two arrays (our initial array + the list of files) $items = array_merge($items, $files); // The list of files should be returned each on a new line, // so we convert this array into a string and we separate values by a new line (\n). return implode("\n", $items); //</code>
Note:
- The "Please Select" string will appear at the top of the list, the "[c]" option making it the default selected item.
- The code tags(//<code> and //</code>) are mandatory for the script to work.
6 persons found this article helpful.
You Should Also Read
Custom PHP code HOT |
Display PHP variables by default when form is shown HOT |
Custom validation rules HOT |
PHP Scripts HOT |
Controlling the calendar HOT |