surtarso / single-file-php-file-browser

Beautify the default HTML directory listing with a single file.
3 stars 3 forks source link

Filter out dotfiles #2

Open ZweiEuro opened 1 month ago

ZweiEuro commented 1 month ago

For me its a quite common use-case to commit a general structure for directories even if they are empty with .gitkeep files or .gitignore files.

I'd suggest puttting this block:

if (strpos($file, '.') == 0 && !is_dir($file)) {
    // Remove all files starting with '.' that are not directories
    continue;
}

Right after listing all directories's files at https://github.com/surtarso/single-file-php-file-browser/blob/f6986022437e10b5465ae69bd635378d0ce4f46e/index.php#L398

But since I understand that's just flavor and not general I just wrote it as an issue and not a PR.

surtarso commented 1 month ago

That would keep .git dirs, but wouldn't that still remove the .gitignore files?

ZweiEuro commented 1 month ago

Well yes, but i wouldn't expect someone to put your index.php directly into another repo/submodule. I think that would be a bad idea in general, but yes my code only filters out files starting with dot.

But you could easily adapt this to filter out anything starting with '.' just by removing && !is_dir statement

surtarso commented 1 month ago

on line 404: if ($file != '.' && $file != '..' && !in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), $notAllowedExtensions)) I thought it was already excluding anything starting with . (files or dirs) and .. (so you cant navigate back the root folder), didnt really check if that was working since it was for the extensions array >.< I added that to hide the hidden files now and the folders are kept, thanks =)

surtarso commented 1 month ago

Actually, adding that line did the trick only for files in the root directory, but it also made the script stop showing inner subfolders for some reason. I reverted the file back.

ZweiEuro commented 1 month ago

I am guessing you checked the relative path of for a dot at the start, which will be true for every path. If you open a branch with the bug you describe i can take a look at it