PHP code example of minhyung / mybox

1. Go to this page and download the library: Download minhyung/mybox 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/ */

    

minhyung / mybox example snippets


use Minhyung\Mybox\MyboxClient;

$mybox = MyboxClient::create($_ENV['MYBOX_PAT']);

// How much room is left?
$storage = $mybox->drive()->storage();
printf("%.1f GB of %.1f GB used\n", $storage->usedBytes / 1e9, $storage->quotaBytes / 1e9);

// Create a folder and put a file in it
$folder = $mybox->files()->createFolder('업무자료');
$mybox->upload()->fromFile('/tmp/report.pdf', parentId: $folder->resourceId);

// Walk the whole folder, one page at a time, without holding it all in memory
foreach ($mybox->drive()->listFolderAll($folder->resourceId) as $item) {
    printf("%-40s %10d bytes\n", $item->name, $item->size);
}

use Minhyung\Mybox\Model\Enum\SortField;
use Minhyung\Mybox\Model\Enum\SortOrder;
use Minhyung\Mybox\Request\ListOptions;

$page = $mybox->drive()->listRoot(new ListOptions(
    sortBy: SortField::ModifiedAt,
    sortOrder: SortOrder::Desc,
    count: 500,          // 1–1000, MYBOX defaults to 100
));

$page->fileCount;        // files directly in this folder
$page->subFolderCount;   // folders directly in this folder
$page->nextCursor();     // null on the last page

$all   = $mybox->drive()->listRootAll();

foreach ($all as $item) { /* one item at a time, pages fetched lazily */ }
foreach ($all->pages() as $page) { /* per-page metadata */ }

$all->take(50);   // stops requesting as soon as it has 50
$all->all();      // everything, eagerly

// From a local file — reserves the URL and pushes the bytes in one call
$mybox->upload()->fromFile('/tmp/report.pdf', parentId: $folderId, isOverwrite: true);

// From memory
$mybox->upload()->fromString($csv, 'export.csv', parentId: $folderId);

// From an open stream, when you know the exact length
$mybox->upload()->fromStream($handle, 'video.mp4', $size, parentId: $folderId);

// Ask MYBOX whether it kept part of an interrupted upload, and continue from there
$mybox->upload()->fromFile('/tmp/big.zip', resume: true);

$mybox->download()->toFile($fileId, '/tmp/report.pdf');   // streamed to disk
$mybox->download()->contents($fileId);                    // into memory
$stream = $mybox->download()->open($fileId);              // PSR-7 stream

use Minhyung\Mybox\Model\Enum\Category;
use Minhyung\Mybox\Request\SearchFilesOptions;

foreach ($mybox->search()->filesAll(new SearchFilesOptions(
    q: '1월 회의록 pdf',
    category: Category::Document,
    parentPath: '/문서/',
)) as $file) {
    echo $file->path, PHP_EOL;
}

$folderId = $mybox->paths()->folderId('/문서/2026');
$fileId   = $mybox->paths()->fileId('/문서/2026/회의록.pdf');

use Minhyung\Mybox\Exception\ApiException;
use Minhyung\Mybox\Exception\InsufficientStorageException;
use Minhyung\Mybox\Exception\NotFoundException;
use Minhyung\Mybox\Exception\RateLimitException;

try {
    $mybox->drive()->get($resourceId);
} catch (NotFoundException) {
    // 404
} catch (RateLimitException $e) {
    sleep($e->retryAfter ?? 60);
} catch (InsufficientStorageException) {
    // 507 — the account is full
} catch (ApiException $e) {
    error_log("{$e->errorCode} {$e->errorMessage} (requestId {$e->requestId})");
}

use Minhyung\Mybox\ClientConfig;
use Minhyung\Mybox\Http\RetryPolicy;
use Minhyung\Mybox\MyboxClient;

$mybox = new MyboxClient(new ClientConfig($token, retryPolicy: new RetryPolicy(
    maxAttempts: 5,
    baseDelaySeconds: 1.0,
)));

// Or turn it off entirely
new ClientConfig($token, retryPolicy: RetryPolicy::none());

$mybox = new MyboxClient(
    new ClientConfig($token, userAgent: 'my-app/2.0'),
    httpClient: $myPsr18Client,
    requestFactory: $myPsr17Factory,
    streamFactory: $myPsr17Factory,
);

use League\Flysystem\Filesystem;
use Minhyung\Flysystem\Mybox\MyboxAdapter;

$filesystem = new Filesystem(new MyboxAdapter(MyboxClient::create($token)));