DEV Community

Cover image for PART# 2 - Efficient File Transfer System Using HTTP for Large Datasets - Range Requests
MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on

PART# 2 - Efficient File Transfer System Using HTTP for Large Datasets - Range Requests

PART# 1 - Efficient File Transfer System Using HTTP for Large Datasets - Chunked Uploads

The Range Requests feature allows clients to download specific portions of a file rather than the entire file. This is useful when:

  • Resuming interrupted downloads.
  • Downloading only a portion of a large file (e.g., video streaming).
  • Reducing bandwidth usage by downloading files in chunks.

The given code sets up a system to handle file downloads with support for range requests, ensuring that large files can be downloaded in chunks or resumed from where they left off.


HTML & JavaScript Code Explanation

  • HTML Structure: This file creates a basic webpage with Bootstrap styling. It contains a card titled "Downloads" with a list (<ul>) for displaying available files for download.
  • CSS & JS Files: External CSS (Bootstrap) and JS (jQuery, Bootstrap) files are linked for styling and interactivity.
  • JavaScript File: The chunked-file-upload.js script will handle the logic for fetching the available files and managing download requests.

PHP Code: Handling File Downloads (Range Requests)

  • Purpose: This script handles file downloads with support for partial downloads (i.e., range requests).

  • Main Flow:

    1. Receiving the File: It checks if a file parameter is present in the URL ($_GET['file']). If it exists, it extracts the file name using basename() and constructs the file path in ../assets/uploads/.
    2. File Existence: It checks if the file exists using file_exists(). If not, a 404 error is returned.
    3. Range Handling:
      • The script checks if the HTTP Range header is present. This header is sent when the client requests only part of the file (i.e., for resuming interrupted downloads).
      • Using a regular expression, it extracts the start and optional end positions of the byte range.
      • If a range is provided, it sends a 206 Partial Content response and adjusts headers accordingly (Content-Range).
    4. File Streaming:
      • Using fseek(), the script moves the file pointer to the correct byte offset.
      • The file is read and sent to the client in chunks (8KB by default), ensuring efficient memory usage.
    5. Headers: It sends appropriate headers to indicate that the file is downloadable (Content-Disposition) and supports range requests (Accept-Ranges).

Why Use It?

  • Efficient Large File Handling: It allows the server to send only the required parts of a file, making it possible to resume interrupted downloads or request specific parts.
  • Client-Side Control: Modern browsers can handle range requests, allowing users to restart downloads without fetching the entire file again.

PHP Code: Fetching Files for Download

  • Purpose: This script returns a list of available files in the upload directory as a JSON array. The files can then be listed on the client-side for selection.

  • Main Flow:

    1. Directory Scanning: It scans the ../assets/uploads/ directory using scandir() to get all files.
    2. File Filtering: It removes . (current directory), .. (parent directory), and .gitignore from the list using array_diff().
    3. Return as JSON: The cleaned file list is returned as a JSON-encoded array for easy use by JavaScript on the front-end.

Why Use It?

  • Dynamic File Listing: This script dynamically retrieves files, allowing users to download any file available in the upload directory without manually updating the HTML.
  • Simplified Client-Side Interaction: The JSON response is easy to work with using JavaScript, making it simple to display files in a user-friendly format (e.g., a list of clickable links).

Final Summary

  • HTML/JS: Provides a user interface to display downloadable files. The list of files is fetched dynamically from the server.
  • PHP (download.php): Manages the actual file download process, supporting range requests for partial downloads. It efficiently streams files in chunks to avoid high memory usage.
  • PHP (get_files.php): Retrieves the list of available files in the upload directory and returns it as a JSON array for the front-end to display.

The range request functionality ensures efficient and reliable downloads, especially for large files or interrupted downloads, enhancing the user experience.

Connecting Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

Source Code

Top comments (0)