A simple PHP module for handling file uploads with ease.
You can easily install the Upload module using Composer. Run the following command:
composer require lithemod/uploadHere's a basic example of how to use the Upload module after installation:
-
Handling File Uploads: Create an HTML form for file uploads:
<form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="fileToUpload" /> <input type="submit" value="Upload" /> </form>
-
Processing the Upload: In your
upload.php, use theUploadclass to handle the file:<?php require 'vendor/autoload.php'; // Include Composer's autoloader use Lithe\Base\Upload; if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Create an instance of the Upload class with the uploaded file $upload = new Upload($_FILES['fileToUpload']); // Specify the upload directory and allowed extensions $uploadDir = 'uploads'; $allowedExtensions = ['jpg', 'png', 'gif', 'txt']; // Move the uploaded file $filePath = $upload->move($uploadDir, $allowedExtensions); if ($filePath) { echo "File uploaded successfully: $filePath"; } else { echo "File upload failed."; } }
- Initializes the upload instance with the file data.
- Moves the uploaded file to the specified directory.
- Generates a unique filename to avoid overwriting.
- Optionally validates the file extension against allowed extensions.
- Checks if a file is successfully uploaded.
- Retrieves the MIME type of the uploaded file.
- Retrieves the size of the uploaded file in bytes.
The module throws exceptions for various error scenarios, including:
- Invalid file upload array
- Upload directory does not exist or is not writable
- File extension not allowed
Make sure to wrap the usage of the module in try-catch blocks to handle exceptions appropriately.
try {
// Upload handling code
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}This module is licensed under the MIT License. See the LICENSE file for more information.