PHP code example of compolomus / binary-file-storage

1. Go to this page and download the library: Download compolomus/binary-file-storage library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

compolomus / binary-file-storage example snippets



use Compolomus\BinaryFileStorage\Storage;

es',
    '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 '<h1>File not found</h1>';
        exit;
    }
}