PHPackages                             compolomus/binary-file-storage - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. compolomus/binary-file-storage

ActiveLibrary[File &amp; Storage](/categories/file-storage)

compolomus/binary-file-storage
==============================

Light file storage

v1.0(8y ago)319MITPHPPHP ^7.0

Since Sep 21Pushed 8y ago2 watchersCompare

[ Source](https://github.com/Compolomus/BinaryFileStorage)[ Packagist](https://packagist.org/packages/compolomus/binary-file-storage)[ RSS](/packages/compolomus-binary-file-storage/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (5)Used By (0)

Koenig BinaryFileStorage
========================

[](#koenig-binaryfilestorage)

[![License](https://camo.githubusercontent.com/f8b99c3a1dbdc976905ddd6cef044a04d9f1b561871887db7479d78ea5828a3c/68747470733a2f2f706f7365722e707567782e6f72672f636f6d706f6c6f6d75732f62696e6172792d66696c652d73746f726167652f6c6963656e7365)](https://packagist.org/packages/compolomus/binary-file-storage)

[![Build Status](https://camo.githubusercontent.com/6e3d630e25fcc9a6210d482ef63e15382abcbf0f22e64e93df357e49a950a1bd/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f436f6d706f6c6f6d75732f42696e61727946696c6553746f726167652f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Compolomus/BinaryFileStorage/build-status/master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/66de188f863db5768a5df7bcf8b18b00e4a07c5e0210e3d0664bb0ab2640f7ce/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f436f6d706f6c6f6d75732f42696e61727946696c6553746f726167652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Compolomus/BinaryFileStorage/?branch=master)[![Code Climate](https://camo.githubusercontent.com/268cf5b6b1273f61ac10d0cfecc5ab28d5fd79533a2aa0a2cefccb0b44f014fb/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f436f6d706f6c6f6d75732f42696e61727946696c6553746f726167652f6261646765732f6770612e737667)](https://codeclimate.com/github/Compolomus/BinaryFileStorage)[![SensioLabsInsight](https://camo.githubusercontent.com/db4505e301b9eaa521388fa8223c76eaa4ba75c20b2158b6ca71614f72014f69/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f32623836306332612d613537332d343561612d396533332d6435393764353930376263302f6d696e692e706e67)](https://insight.sensiolabs.com/projects/2b860c2a-a573-45aa-9e33-d597d5907bc0)[![Downloads](https://camo.githubusercontent.com/776fafe59e57379f7ba31c1eddb97b031613a72f7deade7b9d7e5c3a45dc92b2/68747470733a2f2f706f7365722e707567782e6f72672f636f6d706f6c6f6d75732f62696e6172792d66696c652d73746f726167652f646f776e6c6f616473)](https://packagist.org/packages/compolomus/binary-file-storage)

Перехват выгрузки файлов с форм, добавление в хранилище в бинарном виде, получение файлов по хэш ключу
------------------------------------------------------------------------------------------------------

[](#перехват-выгрузки-файлов-с-форм-добавление-в-хранилище-в-бинарном-виде-получение-файлов-по-хэш-ключу)

Установка:
----------

[](#установка)

composer require compolomus/binary-file-storage

Применение:
-----------

[](#применение)

```
use Compolomus\BinaryFileStorage\Storage;

require __DIR__ . '/vendor/autoload.php';

$storageConfig = [
    'uploadDir' => 'files',
    'prefix' => 'prefix',
    'firstDirLen' => 1,
];

$storage = new Storage($storageConfig);
$input = $storage->check($_FILES);

/*
    Array
(
    [0] => Array
        (
            [name] => 22.png
            [ext] => png
            [size] => 35359
            [type] => image/png
            [path] => files\prefix\7\26\726cd0cefcc522784b2f317ca0affe5f
            [bin] => 726cd0cefcc522784b2f317ca0affe5f
        )

    [1] => Array
        (
            [name] => putty.exe
            [ext] => exe
            [size] => 454656
            [type] => application/x-msdownload
            [path] => files\prefix\9\bb\9bb6826905965c13be1c84cc0ff83f42
            [bin] => 9bb6826905965c13be1c84cc0ff83f42
        )

)
*/

// insert into files ->execute([$input]);

$limit = isset($_GET['limit']) ? abs(intval($_GET['limit'])) : 0;
$file = isset($_GET['file']) ? htmlentities($_GET['file'], ENT_QUOTES, 'UTF-8') : false;
if ($file) {
    $obj = $storage->download($file);
    if (!is_null($obj)) {
        $meta = $storage->getInfoFile($file);
        ob_get_level() && ob_end_clean();
        header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK');
        header('Content-Type: application/force-download');
        header('Content-Description: inline; File Transfer');
        header('Content-Transfer-Encoding: binary');
        if (array_key_exists('name', $meta) & array_key_exists('size', $meta)) {
            header('Content-Disposition: attachment; filename="' . $meta['name'] . '";', false);
            header('Content-Length: ' . $meta['size']);
        }

        $speed = 1024 * $limit;

        if ($speed > 0) {
            $sleep = 1;
        } else {
            $speed = 8 * 1024;
            $sleep = 0;
        }

        while (!$obj->eof()) {
            $buf = $obj->fread($speed);
            print($buf);
            if ($sleep) {
                sleep(1);
            }
            flush();
        }
        exit;
    } else {
        echo 'File not found';
        exit;
    }
}

?>
