delight-im/file-upload

简单方便的文件上传 — 默认安全

v1.2.0 2018-03-13 18:36 UTC

This package is auto-updated.

Last update: 2024-08-24 07:13:55 UTC


README

简单方便的文件上传 — 默认安全

要求

  • PHP 5.6.0+

安装

  1. 通过Composer包含库 [?]

    $ composer require delight-im/file-upload
    
  2. 包含Composer自动加载器

    require __DIR__ . '/vendor/autoload.php';
  3. 设置HTML表单以进行文件上传,例如

    <form action="" method="post" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="1048576">
        <input type="file" name="my-input-name">
        <button type="submit">Upload</button>
    </form>

    元素上必须有两个属性method="post"enctype="multipart/form-data"。同样,必须至少有一个<input type="file">元素,并且具有适当的name属性。最后,需要一种提交表单的方式,例如<button type="submit">元素。隐藏的输入名为MAX_FILE_SIZE是客户端的一个可选提示。

使用方法

文件上传

$upload = new \Delight\FileUpload\FileUpload();
$upload->withTargetDirectory('/my-app/users/' . $userId . '/avatars');
$upload->from('my-input-name');

try {
    $uploadedFile = $upload->save();

    // success

    // $uploadedFile->getFilenameWithExtension()
    // $uploadedFile->getFilename()
    // $uploadedFile->getExtension()
    // $uploadedFile->getDirectory()
    // $uploadedFile->getPath()
    // $uploadedFile->getCanonicalPath()
}
catch (\Delight\FileUpload\Throwable\InputNotFoundException $e) {
    // input not found
}
catch (\Delight\FileUpload\Throwable\InvalidFilenameException $e) {
    // invalid filename
}
catch (\Delight\FileUpload\Throwable\InvalidExtensionException $e) {
    // invalid extension
}
catch (\Delight\FileUpload\Throwable\FileTooLargeException $e) {
    // file too large
}
catch (\Delight\FileUpload\Throwable\UploadCancelledException $e) {
    // upload cancelled
}

限制最大允许文件大小

$upload->withMaximumSizeInBytes(4194304);

// or

$upload->withMaximumSizeInKilobytes(4096);

// or

$upload->withMaximumSizeInMegabytes(4);

读取最大允许文件大小

// e.g. int(4194304)
$upload->getMaximumSizeInBytes();

// or

// e.g. int(4096)
$upload->getMaximumSizeInKilobytes();

// or

// e.g. int(4)
$upload->getMaximumSizeInMegabytes();

限制允许的文件类型或扩展名

$upload->withAllowedExtensions([ 'jpeg', 'jpg', 'png', 'gif' ]);

注意: 默认情况下,使用一组相对安全的文件扩展名,适用于PHP应用程序,并且在互联网上很常见。这可能对某些用例足够。

读取允许的文件类型或扩展名

// e.g. array(4) { [0]=> string(4) "jpeg" [1]=> string(3) "jpg" [2]=> string(3) "png" [3]=> string(3) "gif" }
$upload->getAllowedExtensionsAsArray();

// or

// e.g. string(16) "jpeg,jpg,png,gif"
$upload->getAllowedExtensionsAsMachineString();

// or

// e.g. string(19) "JPEG, JPG, PNG, GIF"
$upload->getAllowedExtensionsAsHumanString();

// or

// e.g. string(21) "JPEG, JPG, PNG or GIF"
$upload->getAllowedExtensionsAsHumanString(' or ');

读取目标目录

// e.g. string(24) "/my-app/users/42/avatars"
$upload->getTargetDirectory();

定义目标文件名

$upload->withTargetFilename('my-picture');

注意: 默认情况下,将使用随机文件名,这在许多情况下是足够(且希望的)。

读取目标文件名

// e.g. string(10) "my-picture"
$upload->getTargetFilename();

读取输入字段名称

// e.g. string(13) "my-input-name"
$upload->getSourceInputName();

Base64上传

$upload = new \Delight\FileUpload\Base64Upload();
$upload->withTargetDirectory('/my-app/users/' . $userId . '/avatars');
$upload->withData($_POST['my-base64']);

try {
    $uploadedFile = $upload->save();

    // success

    // $uploadedFile->getFilenameWithExtension()
    // $uploadedFile->getFilename()
    // $uploadedFile->getExtension()
    // $uploadedFile->getDirectory()
    // $uploadedFile->getPath()
    // $uploadedFile->getCanonicalPath()
}
catch (\Delight\FileUpload\Throwable\InputNotFoundException $e) {
    // input not found
}
catch (\Delight\FileUpload\Throwable\InvalidFilenameException $e) {
    // invalid filename
}
catch (\Delight\FileUpload\Throwable\InvalidExtensionException $e) {
    // invalid extension
}
catch (\Delight\FileUpload\Throwable\FileTooLargeException $e) {
    // file too large
}
catch (\Delight\FileUpload\Throwable\UploadCancelledException $e) {
    // upload cancelled
}

限制最大允许文件大小

$upload->withMaximumSizeInBytes(4194304);

// or

$upload->withMaximumSizeInKilobytes(4096);

// or

$upload->withMaximumSizeInMegabytes(4);

读取最大允许文件大小

// e.g. int(4194304)
$upload->getMaximumSizeInBytes();

// or

// e.g. int(4096)
$upload->getMaximumSizeInKilobytes();

// or

// e.g. int(4)
$upload->getMaximumSizeInMegabytes();

定义文件扩展名

$upload->withFilenameExtension('png');

注意: 这定义了要上传的文件的文件扩展名,这是FileUpload实例的一个属性。它不会更改任何已上传文件的扩展名,这将在File实例中表示。默认情况下,将使用扩展名bin,对于任意(二进制)数据可能足够。

读取文件扩展名

// e.g. string(3) "png"
$upload->getFilenameExtension();

注意: 这检索要上传的文件的文件扩展名,这是FileUpload实例的一个属性。它不会读取任何已上传文件的扩展名,这将在File实例中表示。

读取目标目录

// e.g. string(24) "/my-app/users/42/avatars"
$upload->getTargetDirectory();

定义目标文件名

$upload->withTargetFilename('my-picture');

注意: 默认情况下,将使用随机文件名,这在许多情况下是足够(且希望的)。

读取目标文件名

// e.g. string(10) "my-picture"
$upload->getTargetFilename();

读取Base64数据

// e.g. string(20) "SGVsbG8sIFdvcmxkIQ=="
$upload->getData();

Data URI上传

$upload = new \Delight\FileUpload\DataUriUpload();
$upload->withTargetDirectory('/my-app/users/' . $userId . '/avatars');
$upload->withUri($_POST['my-data-uri']);

try {
    $uploadedFile = $upload->save();

    // success

    // $uploadedFile->getFilenameWithExtension()
    // $uploadedFile->getFilename()
    // $uploadedFile->getExtension()
    // $uploadedFile->getDirectory()
    // $uploadedFile->getPath()
    // $uploadedFile->getCanonicalPath()
}
catch (\Delight\FileUpload\Throwable\InputNotFoundException $e) {
    // input not found
}
catch (\Delight\FileUpload\Throwable\InvalidFilenameException $e) {
    // invalid filename
}
catch (\Delight\FileUpload\Throwable\InvalidExtensionException $e) {
    // invalid extension
}
catch (\Delight\FileUpload\Throwable\FileTooLargeException $e) {
    // file too large
}
catch (\Delight\FileUpload\Throwable\UploadCancelledException $e) {
    // upload cancelled
}

限制最大允许文件大小

$upload->withMaximumSizeInBytes(4194304);

// or

$upload->withMaximumSizeInKilobytes(4096);

// or

$upload->withMaximumSizeInMegabytes(4);

读取最大允许文件大小

// e.g. int(4194304)
$upload->getMaximumSizeInBytes();

// or

// e.g. int(4096)
$upload->getMaximumSizeInKilobytes();

// or

// e.g. int(4)
$upload->getMaximumSizeInMegabytes();

限制允许的MIME类型和扩展名

$upload->withAllowedMimeTypesAndExtensions([
    'image/jpeg' => 'jpg',
    'image/png' => 'png',
    'image/gif' => 'gif'
]);

注意: 默认情况下,使用一组相对安全的MIME类型,适用于PHP应用程序,并且在互联网上很常见。这可能对某些用例足够。

读取允许的MIME类型和扩展名

// e.g. array(3) { [0]=> string(10) "image/jpeg" [1]=> string(9) "image/png" [2]=> string(9) "image/gif" }
$upload->getAllowedMimeTypesAsArray();

// or

// e.g. string(30) "image/jpeg,image/png,image/gif"
$upload->getAllowedMimeTypesAsMachineString();

// or

// e.g. string(32) "image/jpeg, image/png, image/gif"
$upload->getAllowedMimeTypesAsHumanString();

// or

// e.g. string(34) "image/jpeg, image/png or image/gif"
$upload->getAllowedMimeTypesAsHumanString(' or ');

// or

// e.g. array(3) { [0]=> string(3) "jpg" [1]=> string(3) "png" [2]=> string(3) "gif" }
$upload->getAllowedExtensionsAsArray();

// or

// e.g. string(11) "jpg,png,gif"
$upload->getAllowedExtensionsAsMachineString();

// or

// e.g. string(13) "JPG, PNG, GIF"
$upload->getAllowedExtensionsAsHumanString();

// or

// e.g. string(15) "JPG, PNG or GIF"
$upload->getAllowedExtensionsAsHumanString(' or ');

读取目标目录

// e.g. string(24) "/my-app/users/42/avatars"
$upload->getTargetDirectory();

定义目标文件名

$upload->withTargetFilename('my-picture');

注意: 默认情况下,将使用随机文件名,这在许多情况下是足够(且希望的)。

读取目标文件名

// e.g. string(10) "my-picture"
$upload->getTargetFilename();

读取数据URI

// e.g. string(43) "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=="
$upload->getUri();

贡献

欢迎所有贡献!如果您想做出贡献,请首先创建一个问题,以便您的功能、问题或问题可以讨论。

许可证

本项目受MIT许可证条款的约束。