PHP LogoFile Sharing Platform

A File Sharing Platform is a digital system or application designed to facilitate the storage, distribution, and access of digital files among multiple users or entities over a network, typically the internet. These platforms enable users to upload files from their local devices to a centralized server, store them securely, and then share these files with others, either publicly or with specific individuals, often with granular access controls.

Key functionalities of a typical File Sharing Platform include:
1. File Upload: Users can upload various types of files (documents, images, videos, audio, archives) from their computer to the platform's storage.
2. File Storage: Files are stored on a server (local, remote, or cloud-based) and are typically organized for easy retrieval.
3. File Download: Authorized users can download files from the platform back to their local devices.
4. File Management: Users often have features to organize their files, such as creating folders, renaming files, deleting files, or moving them.
5. Sharing Mechanisms:
* Public Links: Generating a unique URL that anyone with the link can use to access or download the file.
* Private Shares: Sharing files with specific registered users or groups, often requiring authentication.
* Access Control: Setting permissions (read-only, edit, download) for shared files.
6. User Authentication & Authorization: Secure login systems to identify users and control their access to files and features.

Advanced features might include:
* Versioning: Keeping multiple versions of a file, allowing users to revert to previous states.
* Preview: In-browser viewing of documents, images, and videos without needing to download them.
* Collaboration: Real-time editing or commenting on shared documents.
* Search Functionality: Tools to quickly find specific files based on name, type, or content.
* Storage Quotas: Limiting the amount of storage space available to individual users or accounts.

Technologically, these platforms leverage web servers (like Apache or Nginx), server-side scripting languages (such as PHP, Python, Node.js, Ruby), databases (like MySQL, PostgreSQL) for managing user data and file metadata, and client-side technologies (HTML, CSS, JavaScript) for the user interface. Security measures like encryption, secure authentication protocols, and regular backups are crucial to protect user data.

Example Code

<?php

// Define the directory where files will be uploaded
$upload_directory = 'uploads/';

// Ensure the upload directory exists and is writable
if (!is_dir($upload_directory)) {
    mkdir($upload_directory, 0777, true); // Create directory with read/write permissions
}

// Handle file upload
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file_to_upload'])) {
    $file_name = basename($_FILES['file_to_upload']['name']);
    $target_file = $upload_directory . $file_name;
    $upload_ok = 1;
    $file_type = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

    // Check if file already exists
    if (file_exists($target_file)) {
        echo "<p style='color:orange;'>Sorry, file already exists. Please rename your file or delete the existing one.</p>";
        $upload_ok = 0;
    }

    // Check file size (e.g., limit to 5MB)
    if ($_FILES['file_to_upload']['size'] > 5000000) {
        echo "<p style='color:orange;'>Sorry, your file is too large.</p>";
        $upload_ok = 0;
    }

    // Allow certain file formats (optional)
    // $allowed_types = array('jpg', 'png', 'jpeg', 'gif', 'pdf', 'doc', 'docx', 'txt', 'zip');
    // if (!in_array($file_type, $allowed_types)) {
    //     echo "<p style='color:orange;'>Sorry, only JPG, JPEG, PNG, GIF, PDF, DOC, DOCX, TXT & ZIP files are allowed.</p>";
    //     $upload_ok = 0;
    // }

    // Check if $upload_ok is set to 0 by an error
    if ($upload_ok == 0) {
        echo "<p style='color:red;'>Sorry, your file was not uploaded.</p>";
    } else {
        if (move_uploaded_file($_FILES['file_to_upload']['tmp_name'], $target_file)) {
            echo "<p style='color:green;'>The file " . htmlspecialchars($file_name) . " has been uploaded.</p>";
        } else {
            echo "<p style='color:red;'>Sorry, there was an error uploading your file.</p>";
        }
    }
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Basic File Sharing Platform</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; background-color: #f4f4f4; }
        .container { max-width: 800px; margin: auto; background: white; padding: 20px; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); }
        h1, h2 { color: #333; }
        form { margin-bottom: 30px; padding: 15px; border: 1px solid #ddd; border-radius: 5px; background-color: #f9f9f9; }
        input[type=\"file\"] { margin-right: 10px; }
        input[type=\"submit\"] {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        input[type=\"submit\"]:hover { background-color: #45a049; }
        ul { list-style-type: none; padding: 0; }
        li { margin-bottom: 8px; background-color: #e9e9e9; padding: 10px; border-radius: 4px; display: flex; justify-content: space-between; align-items: center; }
        li a { text-decoration: none; color: #007bff; font-weight: bold; }
        li a:hover { text-decoration: underline; }
        .download-btn {
            background-color: #007bff;
            color: white;
            padding: 5px 10px;
            border: none;
            border-radius: 3px;
            text-decoration: none;
            font-size: 0.9em;
        }
        .download-btn:hover { background-color: #0056b3; }
    </style>
</head>
<body>

    <div class="container">
        <h1>Basic File Sharing Platform</h1>

        <h2>Upload New File</h2>
        <form action="" method="post" enctype="multipart/form-data">
            Select file to upload:
            <input type="file" name="file_to_upload" id="file_to_upload">
            <input type="submit" value="Upload File" name="submit">
        </form>

        <h2>Available Files</h2>
        <?php
        // Get a list of files in the upload directory
        $files = array_diff(scandir($upload_directory), array('.', '..'));

        if (empty($files)) {
            echo "<p>No files uploaded yet.</p>";
        } else {
            echo "<ul>";
            foreach ($files as $file) {
                // Ensure the file exists and is a regular file before linking
                if (is_file($upload_directory . $file)) {
                    $file_url = htmlspecialchars($upload_directory . $file); // For direct download
                    echo "<li>";
                    echo "<span>" . htmlspecialchars($file) . "</span>";
                    // For direct download, link directly to the file
                    // Use force download headers for better user experience or when dealing with executable files.
                    // For this simple example, we'll just link directly.
                    echo "<a href='" . $file_url . "' class='download-btn' download>Download</a>";
                    echo "</li>";
                }
            }
            echo "</ul>";
        }
        ?>
    </div>

</body>
</html>