codad5/file-helper

文件上传器是一个php包,用于帮助快速、简单、安全地上传文件

v1.0.2 2022-08-08 03:54 UTC

This package is auto-updated.

Last update: 2024-09-19 16:56:17 UTC


README

文件上传器是一个php包,用于帮助快速、简单、安全地上传文件

安装

composer require codad5/file-helper

功能

  • 使用方法链快速且易于使用
  • 良好的错误管理
  • 安全管理
  • 适用于多文件上传和单文件上传

用法

index.php

<form action="./upload.php" method='post' enctype="multipart/form-data">
    <input type="file" name="test[]" multiple>
    <input value="submit" type="submit">
</form>

upload.php

<?php
    include(__DIR__ . '/../vendor/autoload.php');
    
    $file_upload_controller = new Codad5\FileHelper\FileUploader('test', "uploads/");

test - 默认给 $_FILES 数组的名称 - 参考如下 $_FILES['test]

uploads/ - 相对于 upload.php 的上传路径

添加允许的扩展名

#any other extension aid the given will give error or ignored depending on your error settings
$file_upload_controller->add_ext('svg', 'png');

设置文件前缀

# for unique id can be replaced with uniqid('', true)
$file_upload_controller->set_prefix('my prefix');

设置最大和最小文件大小

$file_upload_controller->set_sizes(10000, 20);

10000 - 这是允许的最大文件大小,要忽略 .ini 配置,请设置为 null 20 - 这是允许的最小文件大小,默认值为 0

开启/关闭错误报告

/**
 * This is use to turn on and off reporting of the upload process
 * @param bool $file_error - to report error imbound in file from request if `true` else ignore
 * @param bool $size_error - TO report size related error if `true` else ignore
 * @param bool $type_error - To report error related to file type if `true` else ignore
 */
$file_upload_controller->set_reporting(true, false, true);
  • 第一个参数 $file_error 用于报告请求中文件错误,如 $_SERVER['test'] 中找到的错误
  • 第二个参数 $size_error 用于报告与大小相关的错误,具体取决于您的设置
  • 第三个参数 $type_error 用于报告错误,如果文件不是允许的文件之一

注意:如果任何一个为 false 且发现错误,它将忽略文件并继续上传,不包含该文件

移动文件

$file_upload_controller->move_files();

获取上传文件路径

$upload_path = $file_upload_controller->get_uploads();

foreach ($upload_path as $key => $value) {
        # code...
        echo "This file has been uploaded to ".$value['uploaded_path']."<br/>;
    }
  • 这将返回一个多维数组,每个数组都有一个以下键
  • uploaded_path - 相对于脚本标签 upload.php 的最终上传文件路径
  • name - 文件名称
  • size - 文件大小
  • type - 文件类型
  • ext - 文件扩展名

方法链

$file_upload_controller = new \Codad5\FileHelper\FileUploader('tes', "uploads/");

    $uploaded_file_array = $file_upload_controller
    ->set_reporting(false, false, false)
    ->add_ext('png', 'pdf')
    ->set_prefix('cool Stuff')
    ->move_files()
    ->get_uploads();

    foreach ($uploaded_file_array as $key => $value) {
        # code...
        echo "This file has been uploaded to ".$value['uploaded_path']."<br/>";
    }

随着项目的增长,此文档将及时更新,如需查询更多信息,请联系我 在此

Chibueze Michael A. 用 💗 构建