PHP - Create multidimensional recursive array from string (Files/folders structure) - php

Someone asked same question 10 years ago but there is no answer to that
Reference: Trying to create Array from String (file/folder structure)
Update Jan 8, 2022: This is how array and tree should look like:
https://3v4l.org/6ULBZ#v8.1.1
The SQL output should be formed into this array structure from a string.
[
'id' => $key //Increment from foreach loop,
'file' => $row['name'] //Name of folders and files
'parent_id' => 0 // 0 - if no subfolder, $id if there is subfolder
]
I want to return a tree-level multidimensional array from file list stored in database as a string.
For example I store my work related path in database like this:
SELECT name FROM files;
... SQL Output
2021/Dec/File1.doc
2021/Dec/File2.doc
2021/Dec/File3.doc
2021/Nov/File1.doc
2021/Nov/File2.doc
2021/Nov/File3.doc
2021/Nov/File4.doc
2020/Jan/File1.doc
2020/Jan/File2.doc
2020/Jan/File3.doc
... PHP recursive array output should be categorized by folders, subfolders, more subfolders if exists and files on end.
-2021
--Dec
---File1.doc
---File2.doc
---File3.doc
--Nov
---File1.doc
---File2.doc
---File3.doc
---File4.doc
-2020
--Jan
---File1.doc
---File2.doc
---File3.doc
What would be the best way to achieve this performance wise?
What I got so far...
$files = [];
foreach ($sql as $row)
{
// Separate directories
$separator = explode('/', $row['name']);
/* Output:
Array
(
[0] => 2021
[1] => Dec
[2] => file1.doc
)
*/
// Find the file via regex
if (preg_match('/[^\/]*\.(doc|txt)/', $row['name'], $o))
{
$row['name'] = $o[0]; //Output: file1.doc
}
$files[] = $row;
}
... For now I only have a file names, now I need directories as well and then make a multidimensional array from it.

$files = [];
foreach ($sql as $row)
{
// Separate directories
$separator = explode('/', $row['name']);
/* Output:
Array
(
[0] => 2021
[1] => Dec
[2] => file1.doc
)
*/
if (preg_match('/[^\/]*\.(doc|txt)/', $row['name']))
{
$node = &$files;
while (count($separator) > 1) {
$folder = array_shift($separator);
if (!array_key_exists($folder, $node)) {
$node[$folder] = [];
}
$node = &$node[$folder];
}
$node[] = $separator[0];
}
}

I have one solution for you, I hope this will help.
$files="2021/Dec/File1.doc,
2021/Dec/File2.doc,
2021/Dec/File3.doc,
2021/Nov/File1.doc,
2021/Nov/File2.doc,
2021/Nov/File3.doc,
2021/Nov/File4.doc,
2020/Jan/File1.doc,
2020/Jan/File2.doc,
2020/Jan/File3.doc";
$files=explode(',',$files);
foreach($files as $file)
{
$i=1;
$paths=explode('/',$file);
foreach($paths as $path)
{
for($j=0;$j<$i;++$j){
echo '-';
}
echo $path;
echo "<br/>";
++$i;
}
$i=0;
}
You can use your database files. by removing my files constant value and use your.

Related

get entire folders/files tree and rename

I have a folders/files tree inside admin folder (windows, localhost).
All files are .html.
Each of them (files and folders) is starting with some numbers and middle dash, for example
32-somefolder
624-somefile.html
I need to list all of them and remove all prefixes from their names.
So the result should be:
somefolder
somefile.html
foreach(glob("admin/*") as $el) {
echo $el . '.' . filetype($el) . '<br>';
}
First problem - only folders are listed:
admin/32-somefolder.dir
How to get files too, and how to rename i.e. remove prefixes from all the names?
You can use the second choice to list files : scandir, and recursive function :
function removePrefixFiles($dir, &$results = array()){
$files = scandir($dir);
foreach ($files as $key => $value){
$path = realpath($dir . DIRECTORY_SEPARATOR . $value);
if (! is_dir($path)) {
// treat the filename
$file = pathinfo($path);
$filename = explode('-', $file['filename']);
if (count($filename) > 0) {
// '-' is found, rename file
rename($path, $file['dirname'] .'/'. $filename[1] .'.'. $file['extension'];
}
$results[] = $path;
} else if ($value != '.' && $value != '..') {
removePrefixFiles($path, $results);
$results[] = $path;
}
}
// no real need to return something here, but can log the files
return $results;
}
$dir = '/admin';
removePrefixFiles($dir);
I have created two folder inside admin/ name as
1-files and 2-abc
then inside folder 1-files i have two files
11-java.html
11-text.html
then inside folder 2-abc i have two files
22-php.html
22-sql.html
<?php
$dir = "admin/";
// Sort in ascending order - this is default
$a = scandir($dir);
echo "<pre>";
if(count($a)>0){
$newArr = array();
for($i=2;$i<count($a);$i++){
$test = array();
$folderArr = array();
$folderName = explode('-',$a[$i]);
$test['folder'] = $folderName[1];
$b = scandir($dir.'/'.$a[$i]);
for($j=2;$j<count($b);$j++){
$fileName = explode('-',$b[$j]);
$folderArr[] = substr($fileName[1], 0, strpos($fileName[1], "."));;
}
$test['files'] = $folderArr;
$newArr[] = $test;
}
}
print_r($newArr);
?>
This will be the output
Array
(
[0] => Array
(
[folder] => files
[files] => Array
(
[0] => java
[1] => text
)
)
[1] => Array
(
[folder] => abc
[files] => Array
(
[0] => php
[1] => sql
)
)
)
Hope this willl hellp you.

How to detect changes in directories or file inside that directory using php

I have directory structure like this:
[Products] => Array
(
[Category1] => Array
(
[product1] => Array
(
[documents] => Array
(
[0] => Attachment1.pdf
[1] => Attachment2.pdf
)
)
)
)
if there is any changes in any directory or file inside products directory
like:
product1 changed to product2
new directory added
any changes in file Attachment.pdf
new document added to directory.
What i have tried till now:
$dir="PATH TO PRODUCT DIRECTORY"
$stats = stat($dir);
but $stats['mtime'] detected change in Products children only (Like if change Category1 to Category2 or newly added directory to products) but not in grand children of products (Like if i change product1 to product2) and so on.
Can any one help me to delect any changes in product with children.
Here is my solution i have implemented. By using these function i am able to get a MD5 hash code and i can use this hash code to check if there is any changes in directory structure or in any file.
$dir="PATH TO PRODUCT DIRECTORY";
public function directoryHash($dir, $item_name){
$dirStr=$this->directoryStruture($dir, $separator= DIRECTORY_SEPARATOR, $root='');
$strHash=md5(json_encode($dirStr));
return $strHash;
}
public function directoryStruture($dir, $separator = DIRECTORY_SEPARATOR, $root = '') {
$result = array();
$cdir = scandir($dir);
$result = array();
if ($root === '') {
$root = $dir;
}
$cdir = scandir($dir);
foreach ($cdir as $key => $value) {
if (!in_array($value, array(".", ".."))) {
$current = $dir . $separator . $value;
if (is_dir($current)) {
$result['items'][] = $value;
$result[$value] = $this->directoryStruture($current, $separator, $root);
} else {
$filePath = str_replace($root, '', $current);
$result[] = $filePath.filemtime($current);
}
}
}
return $result;
}
If there is any better approach. Please let me know.
Thanks,
M.

Function with foreach unexpectedly retaining variable's value

I'm trying to grab and print all paths from root to leaf in a nested PHP array. So for example, if this is my array:
$tree = array(
"A" => "w",
"B" => array("x" => array("y", "z"))
)
I want the following as output:
Array
(
[0] => Aw
[1] => Bxy
[2] => Bxz
)
Here's my function at present:
function traverse($input, $myPath) {
global $allPaths;
if(is_array($input)) {
foreach($input as $k => $v) {
if(!is_int($k)) $myPath .= $k;
traverse($v, $myPath);
}
} else {
$myPath .= $input;
$allPaths[] = $myPath;
}
}
When I run this code:
$allPaths = array();
echo "<pre>";
traverse($tree, "");
print_r($allPaths);
echo "</pre>";
The output is this:
Array
(
[0] => Aw
[1] => ABxy
[2] => ABxz
)
That's almost correct, but for some reason the A is being retained when it reaches the "B" part instead of being reset as I would expect.
I've re- and re-read the code and tried every debugging message I can think of, and still don't get what's going on. I'm sure it's either something basic (perhaps so much so that if I haven't seen it yet, I never will) or I'm just not getting how variable scope is working here.
The 1st call to the function runs over the top level array, so the top level keys always get appended.
To fix, pass a copy of $myPath with $k appened to the recursive calls, rather than appending to $myPath directly:
function traverse($input, $myPath) {
global $allPaths;
if(is_array($input)) {
foreach($input as $k => $v) {
if(!is_int($k)){
traverse($v, $myPath . $k);
}else{
traverse($v, $myPath);
}
}
} else {
$myPath .= $input;
$allPaths[] = $myPath;
}
}

Adding a second dimension to an exisiting PHP array

Language: PHP 5
Framework: Joomla 3
I have a single dimension array that I get from get_file_array(). I need to break down the names of the files and eventually query the database for more information. For ease of iteration, I was hoping to add a new dimension to my array rather than create parallel arrays. I thought of iterating through the existing array and adding it one node at a time to a new, specifically multidimensional, perhaps associative array, but this seems inelegant. Is there a way to add a dimension to an existing array in PHP? Here's what I tried last, which obviously doesn't work but conveys the spirit of what I want to accomplish:
require_once "../components/com_esms/models/officelookup.php";
class filearray extends JApplicationCli
{
var $dir = null;
//var_dump($dir);
//error_log("filecopy CWD: ".getcwd());
//error_log("filecopy files: ".print_r($dir, true));
function __construct()
{
$this->dir = scandir("../esms_reports/", 0);
parent::__construct();
}
public function get_file_array()
{
//$this->out('Hello World');
unset($this->dir[0]);
unset($this->dir[1]);
$fa = array_values($this->dir);
return $fa;
}
}
$arr_filearray_obj = new filearray();
$dir = $arr_filearray_obj->get_file_array();
//error_log("filecopy files: ".print_r($dir, true));
/*
foreach($dir as $filename)
{
$fa_underscore = explode("_", $filename);
error_log("filecopy lotid: ".$fa_underscore[1]);
}
*/
//$officeid = array();
for($i = 0; $i < count($dir); $i++)
{
$fa_underscore = explode("_", $dir[$i]);
//error_log("filecopy lotid: ".$fa_underscore[1]);
//error_log("filecopy number of lots: ".$i);
$fa_dash = explode("-", $fa_underscore[1]);
$dir[i][1] = $fa_dash[1];
}
error_log("filecopy officeids: ".print_r($dir, true));
//$result = file_get_contents("http://192.168.1.250/systemname/index.php/option=com_esms&view=officelookup&format=json&officeID=".$officeid[0]);
$officelookup = new EsmsModelofficelookup();
for($o = 0; $o < count($dir); $o++)
{
$result = $officelookup->get_offices($dir[$o][1]);
}
error_log("filecopy JSON: ".$result);
echo("DONE!");
EDIT: Here's an example of the file names I am manipulating. The point is to get the client id, query it, check if it has a parent client id and make a folder structure based on that, using the client names, not IDs.
DDR_1426112931-429_031215.pdf or typeofreport_lotid-clientid_date.pdf
What I would want to add to the array would be the results of my database query, which is a JSON encoded structure containing the client's information and the parent client information, if it exists. It looks something like this:
Array
(
[id] => 123
[name] => Dummy
[parent] => 321
)
Array
(
[id] => 321
[name] => DummyParent
[parent] =>
)

PHP recursive folder scan into multi array (subfolders and files)

I'm a little bit lost here at the moment. My goal is to recursive scan a folder with subfolders and images in each subfolder, to get it into a multidimensional array and then to be able to parse each subfolder with its containing images.
I have the following starting code which is basically scanning each subfolders containing files and just lost to get it into a multi array now.
$dir = 'data/uploads/farbmuster';
$results = array();
if(is_dir($dir)) {
$iterator = new RecursiveDirectoryIterator($dir);
foreach(new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) {
if($file->isFile()) {
$thispath = str_replace('\\','/',$file->getPath());
$thisfile = utf8_encode($file->getFilename());
$results[] = 'path: ' . $thispath. ', filename: ' . $thisfile;
}
}
}
Can someone help me with this?
Thanks in advance!
You can try
$dir = 'test/';
$results = array();
if (is_dir($dir)) {
$iterator = new RecursiveDirectoryIterator($dir);
foreach ( new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file ) {
if ($file->isFile()) {
$thispath = str_replace('\\', '/', $file);
$thisfile = utf8_encode($file->getFilename());
$results = array_merge_recursive($results, pathToArray($thispath));
}
}
}
echo "<pre>";
print_r($results);
Output
Array
(
[test] => Array
(
[css] => Array
(
[0] => a.css
[1] => b.css
[2] => c.css
[3] => css.php
[4] => css.run.php
)
[CSV] => Array
(
[0] => abc.csv
)
[image] => Array
(
[0] => a.jpg
[1] => ab.jpg
[2] => a_rgb_0.jpg
[3] => a_rgb_1.jpg
[4] => a_rgb_2.jpg
[5] => f.jpg
)
[img] => Array
(
[users] => Array
(
[0] => a.jpg
[1] => a_rgb_0.jpg
)
)
)
Function Used
function pathToArray($path , $separator = '/') {
if (($pos = strpos($path, $separator)) === false) {
return array($path);
}
return array(substr($path, 0, $pos) => pathToArray(substr($path, $pos + 1)));
}
RecursiveDirectoryIterator scans recursively into a flat structure. To create a deep structure you need a recursive function (calls itself) using DirectoryIterator. And if your current file isDir() and !isDot() go deep on it by calling the function again with the new directory as arguments. And append the new array to your current set.
If you can't handle this holler and I'll dump some code here. Have to document (has ninja comments right now) it a bit so... trying my luck the lazy way, with instructions.
CODE:
/**
* List files and folders inside a directory into a deep array.
*
* #param string $Path
* #return array/null
*/
function EnumFiles($Path){
// Validate argument
if(!is_string($Path) or !strlen($Path = trim($Path))){
trigger_error('$Path must be a non-empty trimmed string.', E_USER_WARNING);
return null;
}
// If we get a file as argument, resolve its folder
if(!is_dir($Path) and is_file($Path)){
$Path = dirname($Path);
}
// Validate folder-ness
if(!is_dir($Path) or !($Path = realpath($Path))){
trigger_error('$Path must be an existing directory.', E_USER_WARNING);
return null;
}
// Store initial Path for relative Paths (second argument is reserved)
$RootPath = (func_num_args() > 1) ? func_get_arg(1) : $Path;
$RootPathLen = strlen($RootPath);
// Prepare the array of files
$Files = array();
$Iterator = new DirectoryIterator($Path);
foreach($Iterator as /** #var \SplFileInfo */ $File){
if($File->isDot()) continue; // Skip . and ..
if($File->isLink() or (!$File->isDir() and !$File->isFile())) continue; // Skip links & other stuff
$FilePath = $File->getPathname();
$RelativePath = str_replace('\\', '/', substr($FilePath, $RootPathLen));
$Files[$RelativePath] = $FilePath; // Files are string
if(!$File->isDir()) continue;
// Calls itself recursively [regardless of name :)]
$SubFiles = call_user_func(__FUNCTION__, $FilePath, $RootPath);
$Files[$RelativePath] = $SubFiles; // Folders are arrays
}
return $Files; // Return the tree
}
Test its output and figure it out :) You can do it!
If you want to get list of files with subdirectories, then use (but change the foldername)
<?php
$path = realpath('yourfold/samplefolder');
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename)
{
echo "$filename\n";
}
?>

Categories