Answer the question
In order to leave comments, you need to log in
Why does the function write 2 times?
There is a folder with subfolders. In the final files are located, the links (paths) to which must be written to the file and this file must be in the final folder.
There is a function that iterates through folders recursively and writes the paths to a file.
function build($dir) {
foreach (scandir($dir) as $node) {
if ($node == '.' || $node == '..') continue;
if (is_dir($dir . DIRECTORY_SEPARATOR . $node)) {
build($dir .DIRECTORY_SEPARATOR . $node);
} else {
$dirname = dirname($dir . DIRECTORY_SEPARATOR . $node).DIRECTORY_SEPARATOR;
if ($node !== "index.txt"){
var_dump($dirname.$node);
$idxFile = $dirname.'index.txt';
file_put_contents($idxFile, $dirname.$node. "\n",FILE_APPEND);
}
}
}
}
1.txt
2.bat
3.dll
1.txt
2.bat
3.dll
Answer the question
In order to leave comments, you need to log in
First, replace dirname()
with realpath()
. Secondly, move the second DIRECTORY_SEPARATOR inside the parentheses realpath()
. And third, don't create new files in the directory you're reading from. Collect the output into a variable, and after scanning the directory (i.e. outside foreach (scandir($dir) as $node) {}
) create a file in the scanned directory.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question