surtarso / single-file-php-file-browser

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

Single File PHP File Browser

Table of Contents

  1. Introduction
  2. Functionality
  3. Screenshot
  4. Usage
  5. Technologies Used
  6. Best Practices
  7. Code Explanation
  8. Roadmap

Introduction

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.

Back to top

Functionality

This project offers the following functionalities:

Back to top

Screenshot

Single File PHP File Browser Screenshot

(*Folders with the 'external link' icon will navigate to their own index.php file instance.)

Back to top

Usage

To use the Single File PHP File Browser, follow these simple steps:

  1. Make sure you have PHP enabled on your webserver.

  2. Download the index.php file from this project.

  3. Place the index.php file in the folder you want to share on the web.

  4. 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.

Uploads (Optional)

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:

Back to top

Technologies Used

Back to top

Best Practices

To maintain simplicity and effectiveness, this project follows some best practices:

Back to top

Code Explanation

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:

Function listDirectory

function listDirectory($directory) {
    // ...
}

Scanning Directory Contents

$files = scandir($directory);

Excluded File Extensions

$notAllowedExtensions = array('html', 'php', 'swp', 'css');

File Extension to Icon Mapping

$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
);

Generating the Directory Listing

echo '<ul class="folder-contents">';
foreach ($files as $file) {
    // ...
}
echo '</ul>';

Handling Subdirectories Recursively

if (is_dir($path)) {
    // ...
}

Customizing Icons and File Links

$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>';

Specifying the Directory

$directory = './'; // Specify the directory you want to list

Invoking the Function

listDirectory($directory);

Back to top

Roadmap

Here are some planned enhancements for the Single File PHP File Browser:

Contribute

Feel free to contribute to the project and help make these enhancements a reality.

Back to top