PROBLEM STATEMENT
I have 1000s of files stored in folder structure in the following format
<root_directory>/year/month/day/category/file.ext
Based on certain parameters I need to select one or many top level folder(s) and compress all or selected files from it to one zip file on the go and avail it to the user to downloaded. Now to compress all these files I am using a method shown in solution of this question on StackOverflow
Download multiple files as a zip-file using php - Stack Overflow[
^] .
this method requires an array having full path of files need to compress and that's where I am stucked at.
SITUATION
It's kinda weird what I am facing right now. I have a function that returns an array of all files in a provided folder and its subfolder. Now the problem is when I am
print_r($arr) within the function where
$arr has all the data it's showing me data but when I assign that data to a variable calling function and
print_r($variable) it's an empty array();
I dig in deeper and observed that $variable has multiple arrays assigned in it may be because it's recursive.
Let me give some example code...
<?php
$arrGlobal = array();
function listFolderFiles($dir) {
$ffs = scandir($dir);
$arr = array();
unset($ffs[array_search('.', $ffs, true)]);
unset($ffs[array_search('..', $ffs, true)]);
if (count($ffs) < 1)
return;
foreach($ffs as $ff){
if(is_dir($dir.DIRECTORY_SEPARATOR.$ff)) {
listFolderFiles($dir.DIRECTORY_SEPARATOR.$ff);
} else {
array_push($arr, $dir.DIRECTORY_SEPARATOR.$ff);
}
}
print_r($arr);
return $arr;
}
$variable = listFolderFiles("<path of directory>");
print_r($variable);
?>
now I carefully observed the output of
print_r($arr); its something like this
Array ( [0] => <path_to_file> [1] => <path_to_file> [2] => <path_to_file> )
Array ( [0] => <path_to_file> [1] => <path_to_file> )
Array ( [0] => <path_to_file> [1] => <path_to_file> [2] => <path_to_file> )
Array ( [0] => <path_to_file> [1] => <path_to_file> [2] => <path_to_file> [3] => <path_to_file> )
clearly, you can see this variable has 4 arrays in it and it's not repetitive data as each array has different elements and the different number of elements in them. I have also tried with global variable
$arrGlobal but the result is same. when I count() length of the array it gave me different lengths as follow
$count = count($arr)
echo $count;
3234
I thought it's an array of array so I tried
print_r($arr[0]) and it gave me warning.
<path_to_file><path_to_file><path_to_file><path_to_file>
Notice: Undefined offset: 0 in C:\xampp\htdocs\whoistest\api\index.php on line 314
What I have tried:
I hope I have described everything here. I guess since the function is recursive thus this issue is coming. Please help me resolving this issue asap.