almhdy / async-file-uploader
An asynchronous file uploader library in PHP 8.1
Requires
- php: ^8.1
README
Welcome to the AsyncFileUploader project! This amazing PHP library makes it easy to handle file uploads asynchronously with a focus on simplicity and reliability. π
π¦ Features
- β‘ Asynchronous File Handling: Processes file uploads without blocking the server.
- β File Type Validation: Only allow specific file types (JPEG, PNG, PDF).
- ποΈ Custom Upload Directory: Specify your own directory for file uploads.
- π Error Handling: Receive meaningful feedback on upload issues.
- π Max File Size Restriction: Set a maximum file size limit for uploads.
π Installation
You can include AsyncFileUploader
in your project using Composer:
composer require almhdy/async-file-uploader
Ensure your upload directory is writable! π οΈ
π Usage
Here's a quick example of how to utilize the AsyncFileUploader
:
1. Create an Upload Form
Create an HTML form to select a file:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Async File Uploader</title> </head> <body> <h2>Upload a File π€</h2> <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="file" required> <button type="submit">Upload</button> </form> </body> </html>
2. Handle the Upload
In your upload.php
, use the AsyncFileUploader class:
<?php require 'vendor/autoload.php'; use AsyncFileUploader\AsyncFileUploader; $uploadDir = 'uploads'; // Specify your upload directory $allowedTypes = ["image/jpeg", "image/png", "application/pdf"]; // Allowed file types $maxFileSize = 2 * 1024 * 1024; // Set max file size to 2MB $uploader = new AsyncFileUploader($uploadDir, $allowedTypes, $maxFileSize); $response = $uploader->upload(); if (isset($response['error'])) { echo "Error: " . $response['error']; } else { echo "File uploaded successfully! π Path: " . $response['path']; }
π§ Configuration
- Upload Directory: Specify the directory where files will be stored during initialization.
$uploadDir = 'your/custom/directory'; // Set your custom upload directory
- Allowed File Types: Customize the allowed file types in the constructor.
$allowedTypes = ["image/jpeg", "image/png", "application/pdf"];
- Max File Size: Set a maximum file size (in bytes) for uploads.
$maxFileSize = 2 * 1024 * 1024; // Set max file size to 2MB
π‘οΈ File Type Validation
By default, the allowed file types are:
- Images: JPEG, PNG
- Documents: PDF
You can modify the allowed file types by passing them during initialization.
π οΈ Error Handling
The library provides meaningful error messages. Here are some common errors:
- No file uploaded: Check that your form has a file selected.
- Invalid file type: Ensure you have the correct file type.
- Exceeded file size: Ensure your file does not exceed the specified maximum file size.
ποΈ Example Response
Hereβs what you can expect when a file is uploaded:
Successful Upload
{ "success": true, "path": "uploads/your_uploaded_file.jpg" }
Failed Upload
{ "error": "Invalid file type" }
π Contributing
Contributions are welcome! If you'd like to improve AsyncFileUploader, please fork the repository and submit a pull request! π
π License
This project is licensed under the MIT License. See the LICENSE file for details.
π Support
If you have any questions or need assistance, feel free to open an issue or reach out! We are here to help! π