simp/uploader

This is for uploading file in system

v1.0.0 2025-03-08 18:41 UTC

This package is not auto-updated.

Last update: 2025-05-04 17:51:09 UTC


README

The provided PHP-based file upload system offers two primary methods for handling file uploads:

  1. URL-based Uploads: Allows users to upload files by providing a direct URL.
  2. Form-based Uploads: Enables users to upload files through an HTML form.

The system is designed with extensibility and security in mind, utilizing traits and base classes to promote code reuse and maintainability.

Key Components

1. Traits

a. Properties Trait

This trait encapsulates common properties used across different uploader classes, such as:

  • $original_url
  • $parse_filename
  • $parse_filesize
  • $file_type
  • $system_filename
  • $allowed_extensions
  • $allowed_max_size
  • $temp_object
  • $file_object
  • $validated

By defining these properties in a trait, the system ensures consistency and reduces redundancy across uploader classes.

b. CommonFunction Trait

This trait provides common methods shared among uploader classes, including:

  • addAllowedExtension(string $mime_type = 'image/*'): self
  • addAllowedMaxSize(int $allowed_max_size = 500000): self
  • getExtensionFromMime($mime_type)

These methods facilitate setting allowed file extensions, specifying maximum file sizes, and determining file extensions based on MIME types.

2. Base Classes

a. UrlUploader Class

Handles file uploads via direct URLs. Key functionalities include:

  • Setting the original URL.
  • Validating the file by fetching headers to determine MIME type and size.
  • Downloading the file content.
  • Saving the file to a specified destination.

b. FormUploader Class

Manages file uploads through HTML forms. Its responsibilities encompass:

  • Validating the uploaded file's MIME type and size.
  • Moving the uploaded file to a designated directory.

3. Example Implementations

a. Url.php

An example implementation of the UrlUploader class. It demonstrates how to:

  • Initialize the uploader.
  • Set allowed file extensions and maximum size.
  • Add a URL and validate the file.
  • Move the validated file to the desired location.

b. Form.php

An example implementation of the FormUploader class. It illustrates how to:

  • Initialize the uploader.
  • Set allowed file extensions and maximum size.
  • Validate the uploaded file.
  • Move the validated file to the target directory.

4. User Interfaces

a. index.php

Provides a user interface for URL-based uploads. Users can input a direct URL to upload a file.

b. form.php

Offers a user interface for form-based uploads. Users can select and upload files from their local system.

Usage Instructions

URL-based Uploads

  1. Access the URL Upload Interface:

    • Navigate to index.php in your web browser.
  2. Provide the File URL:

    • Enter the direct URL of the file you wish to upload.
  3. Upload Process:

    • The system will validate the file's MIME type and size.
    • If valid, the file will be downloaded and saved to the specified directory.

Form-based Uploads

  1. Access the Form Upload Interface:

    • Navigate to form.php in your web browser.
  2. Select and Upload a File:

    • Choose a file from your local system that meets the validation criteria.
    • Submit the form to upload the file.
  3. Upload Process:

    • The system will validate the uploaded file's MIME type and size.
    • If valid, the file will be moved to the designated directory.

Developer Notes

  • Extensibility: The use of traits (Properties and CommonFunction) promotes code reuse and simplifies the addition of new uploader types. Developers can create new uploaders by combining these traits with specific functionalities.

  • Validation: Both UrlUploader and FormUploader classes emphasize rigorous validation to ensure that only files meeting the specified criteria are processed. This enhances security and reliability.

  • Customization: Developers can easily adjust allowed file extensions and maximum file sizes using the provided methods in the CommonFunction trait.

  • Error Handling: Implement robust error handling to manage upload failures gracefully. Ensure that appropriate messages are displayed to users in case of validation failures or other issues.

By understanding and utilizing these components, developers can effectively implement and customize the file upload system to meet specific application requirements.