simp / uploader
This is for uploading file in system
Requires
- simp/streamwrapper: ^1.0
Requires (Dev)
- symfony/var-dumper: ^7.2
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:
- URL-based Uploads: Allows users to upload files by providing a direct URL.
- 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
-
Access the URL Upload Interface:
- Navigate to
index.php
in your web browser.
- Navigate to
-
Provide the File URL:
- Enter the direct URL of the file you wish to upload.
-
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
-
Access the Form Upload Interface:
- Navigate to
form.php
in your web browser.
- Navigate to
-
Select and Upload a File:
- Choose a file from your local system that meets the validation criteria.
- Submit the form to upload the file.
-
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
andCommonFunction
) 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
andFormUploader
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.