Skip to content
Advertisement

PHP viewing folder content

I am trying to create a file server.

What I am trying to do is -> on the page there will be lots of folders displayed, when you click on one you will be shown its content and if there is a folder inside of this folder you can click on it and it will display the content of that folder.

This is the code I currently have, however I do not know how to proceed further as I am quite new to PHP.

JavaScript

Advertisement

Answer

Below is something I believe will help you out. I’ve create a directory and file listing interface using PHP’s FilesystemIterator, Bootstrap. Written for PHP 8.

Here is the below code in use: enter image description here

This example uses two files, Reader.php and index.php

Reader.php

JavaScript

index.php

JavaScript

The Reader class includes a number of helper methods removeRootFromPath(), addRootToPath() and cleanPath() the combination of these are there to help prevent users from accessing paths outside of your source directory.

For example, if your file system looked something like….

JavaScript

And you were listing the /var/www/src/ directory, with out the path cleaning methods the the user could modify the input to ../../ and end up at your filesystems root which is something you don’t want!

This is something I’ve put together quickly for the sake of this question and should be used as a guide.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement