John Davidson

php - Loop through paths / files in array and remove path

0 comments
Message:


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



My Answer:
the following code:

php
$files = array();
$dir = 'path/to/directory';

function getFiles($dir){
global $files;
$items = scandir($dir);
foreach($items as $item){
if($item != '.' && $item != '..'){
$path = $dir.'/'.$item;
if(is_dir($path)){
getFiles($path);
} else {
$files[] = $path;
}
}
}
}

getFiles($dir);

foreach($files as $file){
$filename = basename($file);
echo $filename . '
';
}


This code will loop through all the files in the specified directory and its subdirectories, and then use the `basename()` function to extract just the filename from the full path. This way, you can easily access and display just the filenames from the array of files.

Rate this post

5 of 5 based on 3707 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us