Loop through paths / files in array and remove path

110 Views Asked by At

i'm working on a Joomla 3.x component, I'm trying to build an array with folders, files and filenames, I can get the Folders and Files , including paths, which I want, but have been unable to get it to return the names using "basename()" to get just the name.ext . I get an error regarding passing an array to "basename()" in lieu of a string, I've tried "foreach" but it only returns the last item in the array.

Below is the code


function getPathContents()  {

  $this->directory = 'some/directory';
  $recursive       = '1';
  $this->recursive = ($recursive == 'true' || $recursive == 'recursive' || $recursive == '1');
  $this->exclude_folders = '';
  $this->exclude_files = '';
  $options = array();
  $filenames = array();
  $filename = basename($filenames);
  $path = $this->directory;

    if (!is_dir($path))
    {
        if (is_dir(JPATH_ROOT . '/' . $path))
        {
            $path = JPATH_ROOT . '/' . $path;
        }
        else
        {
            return;
        }
    }

    $path = JPath::clean($path);
    $folders = JFolder::folders($path, $this->filter, $this->recursive, true);

    // Build the options list from the list of folders.
    if (is_array($folders))
    {

    foreach ($folders as $folder)
    {

        $options['folders'] = $folders;


    $files = JFolder::files($folder, $this->filter, $this->recursive, true);
    if (is_array($files))
    {

        foreach ($files as $file)
        {

        $options['files'] = $files;

        }

    $filenames = JFolder::files($folder, $this->filter, $this->recursive, true);
    if (is_array($filenames))
    {

        foreach ($files as $filename)
        {

        $options['filenames'] = $filename;

        }
    }
    }
    }

    return $options;

}


any help would be great

UPDATE: Below Code will provide list of Folders and additional arrays of files located in each folder. Each file array is labeled with the Folders name.

Code

function getFolersfiles() {

  $this->directory = 'yourpath';
  $recursive       = '1';
  $this->recursive = ($recursive == 'true' || $recursive == 'recursive' || $recursive == '1');
  $this->exclude_folders = '';
  $this->exclude_files = '';
  $path = $this->directory;

    if (!is_dir($path))
    {
        if (is_dir(JPATH_ROOT . '/' . $path))
        {
            $path = JPATH_ROOT . '/' . $path;
        }
        else
        {
            return;
        }
    }

    $path = JPath::clean($path);
    $options2['folders'] = JFolder::folders($path, $this->filter, $this->recursive, true);

    // Build the options list from the list of folders.
    if (is_array($options2))
    {

    foreach ($options2['folders'] as $option2)
    {

    $optionname = basename($option2);
    $options2[$optionname] = JFolder::files($option2, $this->filter, $this->recursive, true);
    //$options2['filepath'] = JFolder::files($option2, $this->filter, $this->recursive, true);

    }

    return $options2;

}


Thank you for the help

0

There are 0 best solutions below