freddyouellette/php-files-normalizer
Composer 安装命令:
composer require freddyouellette/php-files-normalizer
包简介
Normalizes the PHP $_FILES array to a friendlier format.
README 文档
README
$_FILES in PHP is not a friendly array, especially when POSTing multiple files or using file inputs with nested names like layer[][file]. The PHP $_FILES Array Normalizer will normalize the $_FILES array to the following format:
[
'files' => [
0 => [
'name' => 'file1.png',
'type' => 'image/png',
'tmp_name' => '/tmp/phpxbbHdC',
'error' => 0,
'size' => 11393,
],
1 => [
'name' => 'file2.png',
'type' => 'image/png',
'tmp_name' => '/tmp/phpajPldE',
'error' => 0,
'size' => 11393,
],
],
]
And if you are using nested file inputs with names like layer[0][file] and layer[0][other][file], the format will be as follows:
[
'layer' => [
0 => [
'file' => [
0 => [
'name' => 'file1.png',
'type' => 'image/png',
'tmp_name' => '/tmp/phpxbbHdC',
'error' => 0,
'size' => 11393,
],
],
'other' => [
'file' => [
0 => [
'name' => 'file1.png',
'type' => 'image/png',
'tmp_name' => '/tmp/phpxbbHdC',
'error' => 0,
'size' => 11393,
],
],
],
],
],
]
Things to consider:
- The normalizer will treat all file inputs as if they were
multiple. That means that the finalfileslayer will always be an array of files, even if only one file was POSTed. - Don't forget to add the
multipleattribute and[]to the end of your input name when you want multiple files.
Usage
Install via composer:
composer require freddyouellette/php-files-normalizer
and in your php file:
// require composer autoloader require_once __DIR__.'/vendor/autoload.php'; // include the Normalizer namespace use \freddyouellette\PhpFilesNormalizer\PhpFilesNormalizer; // normalize the $_FILES array $files = PhpFilesNormalizer::normalize($_FILES);
If you would rather merge the $_FILES array into an existing one, just pass the existing array as the second argument:
$files = ['other-data' => 12345]; $files = PhpFilesNormalizer::normalize($_FILES, $files);
This can be useful to normalize all data passed from the frontend to a single array. Then your backend controllers can be passed a single array of data instead of having a direct dependency between the controller and the data type it relies on.
$data = array_merge($_POST, $_GET); $data = PhpFilesNormalizer::normalize($_FILES, $data); // some controller which accepts data as an argument $Controller->__invoke($data);
Contributing
I encourage all issues to be submitted through the Issues tab on GitHub. Pull requests are welcome.
freddyouellette/php-files-normalizer 适用场景与选型建议
freddyouellette/php-files-normalizer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 74 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 06 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 freddyouellette/php-files-normalizer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 freddyouellette/php-files-normalizer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 74
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-06-13