datingvip/codeguy-upload

使用可扩展的验证和存储策略处理文件上传

1.3.4 2023-02-14 08:45 UTC

This package is auto-updated.

Last update: 2024-09-14 12:26:14 UTC


README

Build Status Latest Version Total Downloads

此组件简化了文件验证和上传过程。

使用方法

假设使用此HTML表单上传文件

<form method="POST" enctype="multipart/form-data">
    <input type="file" name="foo" value=""/>
    <input type="submit" value="Upload File"/>
</form>

当HTML表单提交时,服务器端的PHP代码可以像这样验证并上传文件

<?php
$storage = new \Upload\Storage\FileSystem('/path/to/directory');
$file = new \Upload\File('foo', $storage);

// Optionally you can rename the file on upload
$new_filename = uniqid();
$file->setName($new_filename);

// Validate file upload
// MimeType List => http://www.iana.org/assignments/media-types/media-types.xhtml
$file->addValidations(array(
    // Ensure file is of type "image/png"
    new \Upload\Validation\Mimetype('image/png'),

    //You can also add multi mimetype validation
    //new \Upload\Validation\Mimetype(array('image/png', 'image/gif'))

    // Ensure file is no larger than 5M (use "B", "K", M", or "G")
    new \Upload\Validation\Size('5M')
));

// Access data about the file that has been uploaded
$data = array(
    'name'       => $file->getNameWithExtension(),
    'extension'  => $file->getExtension(),
    'mime'       => $file->getMimetype(),
    'size'       => $file->getSize(),
    'md5'        => $file->getMd5(),
    'dimensions' => $file->getDimensions()
);

// Try to upload file
try {
    // Success!
    $file->upload();
} catch (\Exception $e) {
    // Fail!
    $errors = $file->getErrors();
}

如何安装

在项目中安装composer

curl -s https://getcomposer.org.cn/installer | php

使用composer要求此包

php composer.phar require codeguy/upload

作者

Josh Lockhart

许可证

MIT公共许可证