Auto-populate a list with the filenames within a folder
In this article we will provide an example script so that you can generate the items of a dropdown type field with the file names from a folder of your choice.
First we will need to add a regular dropdown field to your form, then in the Items attribute add the following code snippet:
//<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. Joomla\CMS\Filesystem\Folder::files() is a Joomla! framework function. $files = Joomla\CMS\Filesystem\Folder::files(JPATH_SITE.'/path/to/folder'); // You can use paths relative to your Joomla! root, which is stored in the constant JPATH_SITE: // $files = Joomla\CMS\Filesystem\Folder::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>
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 |