定制 lithemod/upload 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lithemod/upload

最新稳定版本:v1.0.0

Composer 安装命令:

composer require lithemod/upload

包简介

A versatile module for efficiently managing file uploads in PHP, ensuring seamless integration and robust error handling.

README 文档

README

A simple PHP module for handling file uploads with ease.

Installation

You can easily install the Upload module using Composer. Run the following command:

composer require lithemod/upload

Usage

Here's a basic example of how to use the Upload module after installation:

  1. 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>
  2. Processing the Upload: In your upload.php, use the Upload class 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.";
        }
    }

Methods

__construct(array $file)

  • Initializes the upload instance with the file data.

move(string $uploadDir, ?array $allowedExtensions = null): ?string

  • Moves the uploaded file to the specified directory.
  • Generates a unique filename to avoid overwriting.
  • Optionally validates the file extension against allowed extensions.

isUploaded(): bool

  • Checks if a file is successfully uploaded.

getMimeType(): ?string

  • Retrieves the MIME type of the uploaded file.

getSize(): ?int

  • Retrieves the size of the uploaded file in bytes.

Error Handling

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();
}

License

This module is licensed under the MIT License. See the LICENSE file for more information.

统计信息

  • 总下载量: 290
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固