定制 djiele/multipart 二次开发

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

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

djiele/multipart

Composer 安装命令:

composer require djiele/multipart

包简介

Parse HTTP multipart/form-data request body. Useful for REST APIs.

README 文档

README

  • This class became necessary during the development of a REST API and had no access to server config and install the "apfd" extension (Linux module) (Windows) . This class parses HTTP multipart/form-data request bodies and try to populate $_FILES and $_POST as PHP would do. The idea was to be able to handle very big files with no memory overflow. The php://input stream is read in chunks of 8192 bytes that are written directly to disk, so very small memory footprint.

At this time there is no check on 'post_max_size', 'upload_max_filesize', 'max_file_uploads' and 'max_input_vars'. Maybe in a future release this checks will be done to be compliant with server configuration.

Installation

You can install the package via composer:

composer require djiele/multipart dev-master
Simple usage
require_once __DIR__'./vendor/autoload.php';
use Djiele\Http\MultipartHandler;
$mh = new MultipartHandler();
$mh->populateGlobals();
echo var_export($_POST, true), PHP_EOL;
echo var_export($_FILES, true), PHP_EOL;
Using the package with frameworks

just instanciate the class at the very beginning of the framework boot. You have to make sure that the class object will be destroyed at the very end of script or application. Otherwise the uploaded files will be deleted by the destructor before your script can process them. For frameworks like Laravel or Symfony i usually instanciate the handler as a public member of 'app' and 'kernel' respectively.

Sample code for Laravel

in public/index.php, just after the app is created you can add this little code:

|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

$app = require_once __DIR__.'/../bootstrap/app.php';

### multipartHandler call if request is PUT, PATCH, OR DELETE
if (
	isset($_SERVER['REQUEST_METHOD']) 
	&& in_array($_SERVER['REQUEST_METHOD'], ['PUT', 'PATCH', 'DELETE'])
) {
    $app->multipartHandler = new Djiele\Http\MultipartHandler();
    $app->multipartHandler->populateGlobals();
}
###

At this step the $_POST and $_FILES super globals are a populated as would PHP with regular POST method. The framework can then populate the Request->files collection and gives you the ability to use UploadedFile objects as you would do normally. Note that you can not use the 'move' method since it make use of the 'move_uploaded_file' native function of PHP which checks that files where uploaded during POST request. You can to use UploadedFile::store() or UploadedFile::storeAs() and then delete the temporary file.


$request->file('id')->storeAs('somedir', $uploadedFile->getClientOriginalName());
unlink($uploadedFile->getPathName());

Sample code for symfony

in the file public/index.php just after the kernel is created add the same little code:

use Symfony\Component\Debug\Debug;
use Symfony\Component\HttpFoundation\Request;
use Djiele\Http\MultipartHandler;

require dirname(__DIR__).'/config/bootstrap.php';

if ($_SERVER['APP_DEBUG']) {
    umask(0000);

    Debug::enable();
}

if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? $_ENV['TRUSTED_PROXIES'] ?? false) {
    Request::setTrustedProxies(explode(',', $trustedProxies), Request::HEADER_X_FORWARDED_ALL ^ Request::HEADER_X_FORWARDED_HOST);
}

if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? $_ENV['TRUSTED_HOSTS'] ?? false) {
    Request::setTrustedHosts([$trustedHosts]);
}

$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);

### multipartHandler call if request is PUT, PATCH, OR DELETE
if (
    isset($_SERVER['REQUEST_METHOD']) 
    && in_array($_SERVER['REQUEST_METHOD'], ['PUT', 'PATCH', 'DELETE'])
) {
    $kernel->multipartHandler = new MultipartHandler();
    $kernel->multipartHandler->populateGlobals();
}
###

$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

Same remarks and notices as Laravel

Et voilà!

djiele/multipart 适用场景与选型建议

djiele/multipart 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 774 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 08 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「http」 「rest」 「laravel」 「multipart」 「form-data」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 djiele/multipart 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 djiele/multipart 我们能提供哪些服务?
定制开发 / 二次开发

基于 djiele/multipart 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-08-26