The Single File PHP File Browser is a lightweight and straightforward project that provides a web-based directory listing for the files contained within a specified directory. This project serves as a quick and easy way to share files or documents with others via a web interface. This code provides a flexible way to display and customize directory listings, making it useful for creating file browsers and similar applications.
This project offers the following functionalities:
(*Folders with the 'external link' icon will navigate to their own index.php file instance.)
To use the Single File PHP File Browser, follow these simple steps:
Make sure you have PHP enabled on your webserver.
Download the index.php
file from this project.
Place the index.php
file in the folder you want to share on the web.
Access the folder using a web browser. You can do this by entering the folder's URL in your web browser's address bar. For example, if you placed index.php
in a folder called "stuff" on your web server, you would access it like this: http://yourdomain.com/stuff/
.
The index.php
file will automatically generate a directory listing for the specified folder, allowing you to view and access the contained files and folders via a user-friendly web interface.
To upload files, create a '.users' text file in the same directory as index.php. Each line in the file should contain a 'username:password' pair. This is enought to make the upload section of the page visible and functional.
However, storing passwords in a plain text file is highly insecure as it exposes them to potential breaches. For enhanced security, consider using a more robust password management system or encrypting the password file.
Recommended Security Measures:
To maintain simplicity and effectiveness, this project follows some best practices:
This PHP code snippet is designed to generate a directory listing for a specified directory, presenting its contents in a structured HTML format. The code can be used to showcase files and subdirectories while allowing for customization of icons based on file extensions. Below is a breakdown of how the code works:
listDirectory
function listDirectory($directory) {
// ...
}
listDirectory
is a recursive function that takes the path to a directory as its parameter.$files = scandir($directory);
scandir
is used to retrieve an array of files and directories within the specified $directory
.$notAllowedExtensions = array('html', 'php', 'swp', 'css');
$notAllowedExtensions
, is defined to store file extensions that should be excluded from the listing. These extensions won't be displayed in the directory listing.$iconMapping = array(
'pdf' => 'icon-pdf',
'doc' => 'icon-doc',
'txt' => 'icon-txt',
'zip' => 'icon-zip',
'md' => 'icon-md',
'tar' => 'icon-tar',
'gz' => 'icon-gz',
'sh' => 'icon-sh',
// Add more mappings as needed
);
$iconMapping
is an associative array that maps file extensions to corresponding CSS icon classes. These classes determine the icons displayed next to file names in the directory listing.echo '<ul class="folder-contents">';
foreach ($files as $file) {
// ...
}
echo '</ul>';
<ul>
) with the class "folder-contents" is initiated to structure the directory listing.foreach
loop iterates through the files and directories obtained from $files
.if (is_dir($path)) {
// ...
}
listDirectory
is then called recursively to list the contents of the subdirectory.$iconClass = isset($iconMapping[$extension]) ? $iconMapping[$extension] : 'icon-default';
echo '<li><i class="' . $iconClass . '"></i><a href="https://github.com/surtarso/single-file-php-file-browser/blob/main/' . $file . '">' . $file . '</a></li>';
$iconMapping
array. If an extension is not found in the mapping, it defaults to 'icon-default'.$directory = './'; // Specify the directory you want to list
$directory
variable is set to the path of the directory you want to list.listDirectory($directory);
listDirectory
function is called with the specified directory to generate the directory listing.Here are some planned enhancements for the Single File PHP File Browser:
Feel free to contribute to the project and help make these enhancements a reality.