Skip to content

Instantly share code, notes, and snippets.

@SOHELAHMED7
Created April 26, 2019 18:01
Show Gist options
  • Save SOHELAHMED7/b4f922f0e82361d9e22f7d03108aa2d8 to your computer and use it in GitHub Desktop.
Save SOHELAHMED7/b4f922f0e82361d9e22f7d03108aa2d8 to your computer and use it in GitHub Desktop.
directory traversal in PHP 7.3
<?php
/*
Directory traversal in PHP
see https://www.shellscript.sh/eg/directories/#traverse.sh (bash)
below one is in PHP 7.3 - Ubuntu
usage demo
1. cd <to ptrav.php>
2. $ php ptrav.php
*/
$dir_to_traverse = '.'; // current dir
$indent = 4;
funct($dir_to_traverse, $indent);
function funct($dir_to_traverse, $indent)
{
$res = scandir($dir_to_traverse);
// print_r($res);
foreach ($res as $key => $value) {
if ($value === '.' || $value === '..') {
continue;
}
if (is_dir($dir_to_traverse .'/'. $value)) {
echo str_repeat(' ', $indent)."dir -------- $value" . PHP_EOL;
funct($dir_to_traverse .'/'. $value, $indent+4);
}
if (is_file($dir_to_traverse .'/'. $value)) {
echo str_repeat(' ', $indent)."file -------- $value" . PHP_EOL;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment