almhdy/async-file-uploader

An asynchronous file uploader library in PHP 8.1

V1.0.1 2024-12-22 01:01 UTC

This package is auto-updated.

Last update: 2025-06-17 02:34:26 UTC


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! 😊