fetchFolderFiles (function)

Last updated: Mon, Apr 17, 2023 9:12 am
Return to Knowledgebase

Purpose

Retrieves all the files in a folder with a given file extension.

Location

users/helpers/us_helpers.php

Parameters

# Parameter Data Type Required Description
1 $folder string Yes specifies the folder to retrieve the files from
2 $extension = string, array No specifies the file extension to filter the files by
3 $extension = "php" Yes undocumented

Returns

Data Type Description of Returned Data
array The function returns an associative array with three keys: "files" - An array of strings representing the file names that match the specified file extension(s). "direct" - An array of strings representing the absolute file paths that match the specified file extension(s). "links" - An array of strings representing the relative file paths (URLs) that match the specified file extension(s).

Further Documentation:

The fetchFolderFiles() function is a custom function in the UserSpice PHP application that retrieves all the files in a folder with a given file extension. Here's how the function works:

$folder - This is a required parameter that specifies the folder to retrieve the files from.

$extension = "php" - This is an optional parameter that specifies the file extension to filter the files by. It defaults to "php" if no value is provided. This parameter can be a string or an array of strings.

global $abs_us_root,$us_url_root; - This line makes the global variables $abs_us_root and $us_url_root available within the function.

if(!is_array($extension)){ $extension = (array)$extension; } - This line checks whether the $extension parameter is an array, and converts it to an array if it is not.

$files = []; $links = []; $direct = []; - These lines initialize three arrays to store the file names, links, and absolute paths.

if(substr($folder,-1) != "/"){ $folder = $folder . "/"; } - This line ensures that the folder path ends with a forward slash /.

$linkpath = $us_url_root . $folder; $basepath = $abs_us_root . $linkpath; - These lines define the URL and absolute paths to the folder.

if(is_dir($basepath)) { ... } - This line checks if the folder exists and is a directory.

$scan_arr = scandir($basepath); $files_arr = array_diff($scan_arr, array('.','..') ); - These lines use the scandir() function to retrieve all the files and directories in the folder, and then removes the . and .. directories from the array.

foreach ($files_arr as $file) { ... } - This loop iterates over each file in the folder.

$file_path = $basepath.$file; $file_ext = pathinfo($file_path, PATHINFO_EXTENSION); - These lines define the absolute path of the file and then extract its extension using the pathinfo() function.

if (in_array($file_ext,$extension)) { ... } - This line checks if the file extension matches the $extension parameter.

$files[] = $file; $links[] = $linkpath . $file; $direct[] = $basepath . $file; - These lines add the file name, link, and absolute path to their respective arrays.

$response = ["files"=>$files,"direct"=>$direct,"links"=>$links]; return $response; - This line creates an associative array containing the file names, links, and absolute paths, and then returns the array.

Here's an example of how you might use this function in a PHP script:

// Get all PHP files in the "includes" folder
$folder = "includes";
$extension = "php";
$files = fetchFolderFiles($folder, $extension);

// Print out the file names
foreach ($files["files"] as $file) {
echo "{$file}\n";
}

Assuming that the fetchFolderFiles() function is defined in a file that has been included in the script, this code retrieves all the PHP files in the includes folder and prints out their names. The resulting output would be a list of all the PHP files in the includes folder.