#32 PHP Server Tools for your file management needs on the server
Florian Zeba

Florian Zeba @flnzba

About: Worked and studied in the fields of management consulting, finance, marketing, IT, mechatronics and law. Lead two web-agencys. Niched in on tax-technology.

Location:
Austria
Joined:
Jan 6, 2025

#32 PHP Server Tools for your file management needs on the server

Publish Date: Mar 8
0 4

Introduction

Server administrators and developers frequently need to perform file management operations such as extracting ZIP archives and deleting files or directories. These operations, traditionally performed via command-line interfaces or FTP clients, can be streamlined through browser-based PHP utilities. This article presents a technical analysis of three PHP scripts designed to handle common file management tasks:

  1. extract-zip.php - For extracting ZIP archives
  2. delete-folder.php - For selectively removing directories
  3. delete-script.php - For unified management of both files and folders

We'll examine the architecture, security implementations, and technical advantages of each tool, while providing code analysis and potential implementation scenarios.

Accessing the Tools

These PHP scripts function as standalone web applications that can be accessed through any browser. After uploading the scripts to your server:

http://example-domain.com/extract-zip.php
http://example-domain.com/delete-folder.php
http://example-domain.com/delete-script.php
Enter fullscreen mode Exit fullscreen mode

For local development environments, you can access them via:

http://localhost/extract-zip.php
http://localhost/delete-folder.php
http://localhost/delete-script.php
Enter fullscreen mode Exit fullscreen mode

The scripts are entirely self-contained, requiring no additional setup or configuration files to function. Simply access the URL, and the web interface will load, providing immediate access to the file management tools.

Technical Overview

All three scripts follow a similar architectural pattern:

  1. Configuration Layer: Setting execution parameters and defining base directories
  2. Functional Layer: Core file operation functions with error handling
  3. Request Processing Layer: Capturing and validating user input
  4. Response Layer: Generating human-readable feedback and UI elements

The scripts also implement critical protection mechanisms including path traversal prevention, execution time management, and confirmation steps to prevent accidental data loss.

Script 1: ZIP File Extraction (extract-zip.php)

Technical Architecture

extract-zip.php implements a web interface for the ZipArchive PHP class. The script:

  1. Sets an extended execution time limit to handle large archives
  2. Scans the current directory for ZIP files
  3. Provides a selection interface for users
  4. Extracts the selected archive with detailed error handling
  5. Provides feedback on the extraction process

Code Analysis

1. Execution Configuration

// Set max execution time to 5 minutes (300 seconds)
set_time_limit(300);
Enter fullscreen mode Exit fullscreen mode

This configuration prevents script timeout during extraction of large archives. For production environments with significant ZIP processing requirements, this parameter may need adjustment based on server capabilities and expected file sizes.

2. ZIP File Discovery

function getZipFiles() {
    $zipFiles = [];
    $dir = dirname(__FILE__);
    if ($handle = opendir($dir)) {
        while (false !== ($entry = readdir($handle))) {
            if ($entry != "." && $entry != ".." && pathinfo($entry, PATHINFO_EXTENSION) === 'zip') {
                $zipFiles[] = $entry;
            }
        }
        closedir($handle);
    }
    return $zipFiles;
}
Enter fullscreen mode Exit fullscreen mode

Technical notes:

  • Uses dirname(__FILE__) to ensure consistent path resolution regardless of execution context
  • Leverages pathinfo() with PATHINFO_EXTENSION parameter for reliable file type identification
  • Explicitly excludes directory entries (. and ..)
  • Properly closes directory handles to prevent resource leaks
  • Returns an indexed array for direct iteration in the presentation layer

3. Archive Extraction Engine

function extractZip($zipFile) {
    // Create a ZIP archive object
    $zip = new ZipArchive();

    // Open the ZIP file
    $result = $zip->open($zipFile);

    $extractedFiles = [];

    if ($result === TRUE) {
        // Get current directory (where this script and ZIP file are located)
        $extractPath = dirname(__FILE__);

        // Extract the contents of the ZIP file
        $zip->extractTo($extractPath);

        // Get list of extracted files
        for ($i = 0; $i < $zip->numFiles; $i++) {
            $extractedFiles[] = $zip->getNameIndex($i);
        }

        // Close the ZIP archive
        $zip->close();

        return [
            'success' => true,
            'files' => $extractedFiles
        ];
    } else {
        // Extraction failed, get error message
        switch ($result) {
            case ZipArchive::ER_INCONS:
                $error = 'Inconsistent ZIP archive.';
                break;
            case ZipArchive::ER_MEMORY:
                $error = 'Memory allocation failure.';
                break;
            case ZipArchive::ER_NOENT:
                $error = 'ZIP file not found.';
                break;
            case ZipArchive::ER_NOZIP:
                $error = 'Not a ZIP archive.';
                break;
            case ZipArchive::ER_OPEN:
                $error = 'Could not open file.';
                break;
            case ZipArchive::ER_READ:
                $error = 'Read error.';
                break;
            case ZipArchive::ER_SEEK:
                $error = 'Seek error.';
                break;
            default:
                $error = 'Unknown error (code: ' . $result . ').';
                break;
        }

        return [
            'success' => false,
            'error' => $error
        ];
    }
}
Enter fullscreen mode Exit fullscreen mode

Technical implementation features:

  • Uses the native PHP ZipArchive class for optimal performance
  • Implements granular error handling with specific error codes from the ZipArchive class
  • Builds a comprehensive list of extracted files for verification
  • Follows proper resource management by explicitly closing the ZIP archive
  • Returns a structured array with consistent keys for result processing

4. Security Implementation

// Handle extraction request
$extractionResult = null;
if (isset($_POST['extract']) && isset($_POST['zipFile'])) {
    $zipFile = $_POST['zipFile'];

    // Basic security check to prevent directory traversal
    if (strpos($zipFile, '../') !== false || strpos($zipFile, '..\\') !== false) {
        $extractionResult = [
            'success' => false,
            'error' => 'Invalid file path.'
        ];
    } else {
        $extractionResult = extractZip($zipFile);
    }
}
Enter fullscreen mode Exit fullscreen mode

Security mechanisms:

  • Validates required parameters before processing
  • Implements directory traversal protection by checking for path manipulation sequences
  • Accounts for both Unix (../) and Windows (..\) path separators
  • Returns structured error response on security violations
  • Does not expose system paths in error messages

Technical Limitations

  1. The script extracts all files to the current directory without a configurable destination
  2. Directory traversal protection only checks for direct path manipulation, not encoded variants
  3. Lacks authentication mechanisms for restricting access to authorized users
  4. Does not implement file type verification beyond the .zip extension

Script 2: Folder Deletion Tool (delete-folder.php)

Technical Architecture

delete-folder.php provides a specialized interface for recursively deleting directory structures. The script implements:

  1. Base directory restriction for security
  2. Directory scanning and listing
  3. Multi-step confirmation process
  4. Recursive directory deletion with error handling
  5. User feedback mechanisms

Code Analysis

1. Base Directory Configuration

// Base directory - set this to limit which directories can be accessed
// For safety, default to the current directory
$baseDir = realpath(dirname(__FILE__));
Enter fullscreen mode Exit fullscreen mode

Technical implementation notes:

  • Uses realpath() to resolve the absolute canonical path
  • Prevents symbolic link attacks by resolving to the real filesystem path
  • Sets a restrictive default (current directory) for maximum security

2. Directory Recursion Algorithm

function deleteDirectory($dir) {
    if (!file_exists($dir)) {
        return true;
    }

    if (!is_dir($dir)) {
        return unlink($dir);
    }

    $files = array_diff(scandir($dir), ['.', '..']);

    foreach ($files as $file) {
        $path = $dir . DIRECTORY_SEPARATOR . $file;

        if (is_dir($path)) {
            deleteDirectory($path);
        } else {
            unlink($path);
        }
    }

    return rmdir($dir);
}
Enter fullscreen mode Exit fullscreen mode

Algorithm characteristics:

  • Implements a depth-first traversal of the directory tree
  • Uses recursive function calls for subdirectory processing
  • Handles edge cases (non-existent directories, files misidentified as directories)
  • Uses DIRECTORY_SEPARATOR constant for cross-platform compatibility
  • Returns boolean success indicators for error tracking

3. Path Security Validation

// Security check: Make sure the folder is within the base directory
$realSelectedPath = realpath($selectedFolder);

if ($realSelectedPath === false) {
    $message = "Error: The selected folder does not exist.";
    $messageType = "error";
} elseif (strpos($realSelectedPath, $baseDir) !== 0) {
    $message = "Error: You can only delete folders within the allowed directory.";
    $messageType = "error";
} else {
    // Deletion code here
}
Enter fullscreen mode Exit fullscreen mode

Security mechanisms:

  • Uses realpath() to resolve symbolic links and normalize paths
  • Validates that the resolved path exists in the filesystem
  • Ensures the target path is a subdirectory of the base directory
  • Uses string position comparison (strpos() === 0) to verify directory hierarchy
  • Provides informative error messages without exposing system paths

4. Multi-step Confirmation

// Confirmation step
if (!isset($_POST['confirm'])) {
    $message = "Please confirm deletion of the folder: " . htmlspecialchars($folderDeleted);
    $messageType = "warning";
} else {
    // Attempt deletion
    try {
        if (deleteDirectory($realSelectedPath)) {
            $message = "Successfully deleted folder: " . htmlspecialchars($folderDeleted);
            $messageType = "success";
        } else {
            $message = "Failed to delete folder: " . htmlspecialchars($folderDeleted);
            $messageType = "error";
        }
    } catch (Exception $e) {
        $message = "Error: " . $e->getMessage();
        $messageType = "error";
    }
}
Enter fullscreen mode Exit fullscreen mode

Implementation features:

  • Two-step confirmation process for destructive operations
  • Exception handling for unexpected errors during deletion
  • Consistent messaging format for different result types
  • Uses htmlspecialchars() to prevent XSS vulnerabilities in output

Script 3: Comprehensive File and Folder Management (delete-script.php)

Technical Architecture

delete-script.php provides a unified interface for managing both files and directories. The script:

  1. Scans directories for all content (files and folders)
  2. Presents a table-based interface with type indicators and file sizes
  3. Provides individual delete buttons for each item
  4. Implements the same security features as the folder deletion tool
  5. Adds file type and size metadata for improved user experience

Code Analysis

1. Enhanced File and Folder Discovery

function getFilesAndFolders($directory) {
    $items = [];

    if ($handle = opendir($directory)) {
        while (false !== ($entry = readdir($handle))) {
            if ($entry != "." && $entry != "..") {
                $fullPath = $directory . DIRECTORY_SEPARATOR . $entry;

                // Store relative path to make it easier to display
                $relativePath = str_replace($GLOBALS['baseDir'] . DIRECTORY_SEPARATOR, '', $fullPath);

                // Add type indicator
                $type = is_dir($fullPath) ? 'folder' : 'file';

                // For files, get extension and size
                $extension = '';
                $size = '';
                if ($type === 'file') {
                    $extension = strtolower(pathinfo($entry, PATHINFO_EXTENSION));
                    $size = filesize($fullPath);
                    // Format file size
                    if ($size < 1024) {
                        $size = $size . ' B';
                    } elseif ($size < 1048576) {
                        $size = round($size / 1024, 2) . ' KB';
                    } else {
                        $size = round($size / 1048576, 2) . ' MB';
                    }
                }

                $items[] = [
                    'path' => $fullPath,
                    'name' => $relativePath ?: $entry,
                    'type' => $type,
                    'extension' => $extension,
                    'size' => $size
                ];
            }
        }
        closedir($handle);
    }

    // Sort items: folders first, then files, both alphabetically
    usort($items, function($a, $b) {
        if ($a['type'] === $b['type']) {
            return strcasecmp($a['name'], $b['name']);
        }
        return ($a['type'] === 'folder') ? -1 : 1;
    });

    return $items;
}
Enter fullscreen mode Exit fullscreen mode

Technical implementation features:

  • Collects comprehensive metadata for each filesystem item
  • Implements human-readable file size formatting with appropriate units
  • Uses case-insensitive string comparison for alphabetical sorting
  • Prioritizes directories over files in the listing order
  • Stores both absolute paths (for operations) and relative paths (for display)
  • Uses global variable access for configuration consistency

2. Unified Deletion Handler

// Attempt deletion
try {
    if ($itemType === "folder") {
        $result = deleteDirectory($realSelectedPath);
    } else {
        $result = unlink($realSelectedPath);
    }

    if ($result) {
        $message = "Successfully deleted " . $itemType . ": " . htmlspecialchars($itemDeleted);
        $messageType = "success";
    } else {
        $message = "Failed to delete " . $itemType . ": " . htmlspecialchars($itemDeleted);
        $messageType = "error";
    }
} catch (Exception $e) {
    $message = "Error: " . $e->getMessage();
    $messageType = "error";
}
Enter fullscreen mode Exit fullscreen mode

Implementation characteristics:

  • Conditional logic based on item type
  • Uses native unlink() function for file deletion
  • Reuses the recursive deleteDirectory() function for directories
  • Consistent error handling for both operation types
  • Maintains type-specific messaging for user feedback

3. Enhanced User Interface Elements

<table class="file-list">
    <thead>
        <tr>
            <th>Type</th>
            <th>Name</th>
            <th>Size</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($items as $item): ?>
            <tr>
                <td>
                    <?php if ($item['type'] === 'folder'): ?>
                        <span class="folder-icon">📁</span> Folder
                    <?php elseif ($item['extension'] === 'zip'): ?>
                        <span class="zip-icon">🗜️</span> ZIP File
                    <?php else: ?>
                        <span class="file-icon">📄</span> File
                    <?php endif; ?>
                </td>
                <td><?php echo htmlspecialchars($item['name']); ?></td>
                <td><?php echo $item['size']; ?></td>
                <td>
                    <form method="post" onsubmit="return confirm('Are you sure you want to delete this <?php echo $item['type']; ?>?');">
                        <input type="hidden" name="item" value="<?php echo htmlspecialchars($item['path']); ?>">
                        <button type="submit" name="delete" class="delete-btn">Delete</button>
                    </form>
                </td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>
Enter fullscreen mode Exit fullscreen mode

UI technical features:

  • Table-based layout for structured data presentation
  • Visual icon differentiation for different file types
  • Client-side JavaScript confirmation for deletion operations
  • Form-based action buttons for each item
  • Hidden input fields for maintaining state between requests
  • XSS protection on all dynamically generated content

Technical Applications and Use Cases

1. Web Application Deployment

These tools are valuable in web application deployment scenarios:

  • Package Extraction: extract-zip.php can unpack deployment packages directly on the server
  • Cleanup Operations: delete-folder.php can remove outdated releases or temporary installation files
  • Version Management: delete-script.php provides a unified interface for managing deployment artifacts

Example workflow:

  1. Upload deployment package: deployment-v1.2.3.zip to your server
  2. Access http://example-domain.com/extract-zip.php in your browser
  3. Select deployment-v1.2.3.zip from the dropdown and click "Extract ZIP File"
  4. Verify the extracted files in the success screen
  5. If needed, use http://example-domain.com/delete-script.php to clean up the original ZIP file

Implementation approach:

// Example addition to extract-zip.php for deployment versioning
$deploymentDirectory = 'releases/v' . date('Y.m.d-His');
mkdir($deploymentDirectory, 0755, true);
$zip->extractTo($deploymentDirectory);
Enter fullscreen mode Exit fullscreen mode

2. Content Management Systems

For CMS applications, these scripts provide:

  • Plugin/Theme Installation: Extract plugin or theme ZIP packages to the appropriate directories
  • Media Management: Clean up unused media directories
  • Bulk Content Operations: Delete generated files and directories for expired content

Example workflow:

  1. Download a new theme or plugin as a ZIP file from the developer's site
  2. Upload the ZIP file to your server (via FTP or other means)
  3. Navigate to http://your-cms-site.com/extract-zip.php in your browser
  4. Extract the theme directly to your web root
  5. Access http://your-cms-site.com/delete-folder.php to remove any deprecated themes

Implementation approach:

// Example modification for CMS plugin installation
function extractPlugin($zipFile, $pluginDirectory) {
    $zip = new ZipArchive();
    $result = $zip->open($zipFile);

    if ($result === TRUE) {
        // Create target directory if it doesn't exist
        if (!file_exists($pluginDirectory)) {
            mkdir($pluginDirectory, 0755, true);
        }

        $zip->extractTo($pluginDirectory);
        $zip->close();
        return true;
    }
    return false;
}
Enter fullscreen mode Exit fullscreen mode

3. Server Administration

For server administrators, these tools provide:

  • Remote File Management: Browser-based alternative to FTP or SSH access
  • Disk Space Management: Identify and remove large directories consuming storage
  • Maintenance Operations: Extract updates or clean up log directories

Example workflow:

  1. Upload system updates as a ZIP file to your server
  2. Browse to http://server-ip/admin/extract-zip.php
  3. Extract the updates package to apply changes
  4. Navigate to http://server-ip/admin/delete-script.php to view all files and directories
  5. Clean up temporary directories and outdated backup files

4. Secure File Hosting

For file hosting applications, these tools can be adapted to:

  • User Upload Processing: Extract user-uploaded ZIP files to their storage areas
  • Storage Quota Management: Delete user files to maintain storage quotas
  • Content Organization: Provide structured file management capabilities to end users

Implementation approach:

// Example modification for multi-tenant file hosting
$userDirectory = 'users/' . $userId . '/files/';
$baseDir = realpath($userDirectory); // Restrict operations to user's directory
Enter fullscreen mode Exit fullscreen mode

Technical Enhancements for Production Use

For production deployment, these scripts should be enhanced with:

1. Authentication & Authorization

Note: As currently implemented, these scripts are immediately accessible to anyone who knows the URL. For production environments, it's critical to add authentication before deployment.

// Example basic authentication implementation
session_start();
if (!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true) {
    // Redirect to login page or show authentication form
    header('Location: login.php');
    exit;
}
Enter fullscreen mode Exit fullscreen mode

2. Enhanced Security Validation

// More robust path traversal prevention
function isPathSafe($path) {
    $realPath = realpath($path);
    $basePath = realpath($GLOBALS['baseDir']);

    // Check if path exists and is within base directory
    if ($realPath === false || strpos($realPath, $basePath) !== 0) {
        return false;
    }

    // Check for encoded path traversal attempts
    $decodedPath = urldecode($path);
    if ($path !== $decodedPath && strpos($decodedPath, '..') !== false) {
        return false;
    }

    return true;
}
Enter fullscreen mode Exit fullscreen mode

3. Logging and Audit Trails

// Example logging implementation
function logOperation($operation, $path, $result, $userId) {
    $logEntry = [
        'timestamp' => date('Y-m-d H:i:s'),
        'operation' => $operation,
        'path' => $path,
        'result' => $result ? 'success' : 'failure',
        'user' => $userId,
        'ip' => $_SERVER['REMOTE_ADDR']
    ];

    file_put_contents(
        'file_operations.log',
        json_encode($logEntry) . PHP_EOL,
        FILE_APPEND
    );
}
Enter fullscreen mode Exit fullscreen mode

4. Configurable Base Directories

// Configuration file approach
$config = include 'config.php';
$baseDir = realpath($config['base_directory']);

// Or environment variable approach
$baseDir = realpath(getenv('FILE_MANAGER_BASE_DIR') ?: dirname(__FILE__));
Enter fullscreen mode Exit fullscreen mode

Installation Instructions

To implement these tools on your server:

  1. Download or create the three PHP files with the provided code:
- `extract-zip.php`
- `delete-folder.php`
- `delete-script.php`
Enter fullscreen mode Exit fullscreen mode
  1. Upload the files to your web server using FTP or your preferred file transfer method

  2. Set appropriate permissions:

    chmod 644 extract-zip.php delete-folder.php delete-script.php
    
  3. Access the tools through your web browser:

    http://your-domain.com/extract-zip.php
    http://your-domain.com/delete-folder.php
    http://your-domain.com/delete-script.php
    
  4. For local development with PHP's built-in server:

    php -S localhost:8000
    

    Then access: http://localhost:8000/extract-zip.php

Conclusion

These PHP file management tools demonstrate effective implementation of server-side file operations. The key technical strengths include:

  1. Security-conscious design: Path validation, input sanitization, and restricted operations
  2. Error handling: Comprehensive error detection and user-friendly messaging
  3. Resource management: Proper handling of file handles and memory-intensive operations
  4. Cross-platform compatibility: Using PHP constants and functions that work across operating systems
  5. Browser-based accessibility: Immediate access via standard HTTP URLs without additional configuration

While suitable for administrative use in protected environments, production deployment should include the enhancements outlined above, particularly authentication mechanisms and more robust security validation.

These utilities serve as both practical tools for server management and educational examples of handling filesystem operations securely in PHP applications.

Additional Resources

Files for Download

Read this article and more on fzeba.com.

Comments 4 total

  • david duymelinck
    david duymelinckMar 8, 2025

    Wow, just scripts with HTML in them. That takes me back.
    What is the reason you went for this style of scripts?

    Also serving them as is I would not recommend, even with the added features.
    A quick way to make the name configurable and hide the file extension, is to create a directory with the name of your choice, and rename the file to index.php.

    • Florian Zeba
      Florian ZebaApr 2, 2025

      I just needed some quick tooling on the server. So I created some quick html/php scripts. No fancy libraries or anything.

      These were tools just for personal use. So as always use at your own risk (to all people reading this).

  • Learn Computer Academy
    Learn Computer AcademyMar 8, 2025

    Really solid overview of these PHP file management tools! The technical breakdown of extract-zip.php and its use of ZipArchive is spot-on for simplifying deployments.

    I love how delete-script.php combines file and folder ops with a clean UI—super practical for server admins. One suggestion: adding a file upload feature to extract-zip.php could make it even more versatile for remote management.

    How do you see these scaling for larger file systems?

    • Florian Zeba
      Florian ZebaApr 2, 2025

      Yes. The upload feature is a nice idea. As I alredy was logged in at the FTP Server there was no need for that.

      It's just a small tool for personal use. Not recommended for enterprise high scale ultra large super duber high end systems.

Add comment