PHP code example of sunkan / php-mogilefs

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

    

sunkan / php-mogilefs example snippets



$connection = new MogileFs\Connection([
    '127.0.0.1:7001'
]);

//or

$connection = new MogileFs\Connection([
    [
        'host' => '127.0.0.1',
        'port' => 7001
    ]
]);



$domainClient = new MogileFs\Client\DomainClient($connection);
try {
	$domain = $domainClient->create('example.com');

	$domain->getDomain(); // domain name
	$domain->getClasses(); // array of classes
} catch(MogileFs\Exception $e) {
	$e->getMessage() === 'domain_exists';
}


$domainClient = new MogileFs\Client\DomainClient($connection);
try {
	$response = $domainClient->delete('example.com');
	$response->isSuccess();
} catch(MogileFs\Exception $e) {
	$e->getMessage() === 'domain_not_found';
}


$domainClient = new MogileFs\Client\DomainClient($connection);

$collection = $domainClient->all();

foreach ($collection as $domain) {
	$domain->getDomain(); // domain name
	$domain->getClasses(); // array of classes
}


$classClient = new MogileFs\Client\ClassClient($connection, 'example.com');
//or
$classClient = new MogileFs\Client\ClassClient($connection);
$classClient->setDomain('example.com');

$classClient = new MogileFs\Client\ClassClient($connection, 'example.com');

try {
	$replicationCount = 2;
	$class = $classClient->create('assets', $replicationCount);
	$class->getName(); // class name
	$class->getCount(); // replication count
	$class->getPolicy(); // replication policy
	$class->getHash(); // hash policy
	$class->getDomain(); // domain name
} catch(MogileFs\Exception $e) {
	$e->getMessage() === 'class_exists';
}

$classClient = new MogileFs\Client\ClassClient($connection, 'example.com');

try {
	$newReplicationCount = 4;
	$class = $classClient->update('assets', $replicationCount);
} catch(MogileFs\Exception $e) {
	$e->getMessage() === 'class_not_found';
}

$classClient = new MogileFs\Client\ClassClient($connection, 'example.com');

try {
	$response = $classClient->delete('assets');
	$response->isSuccess();
} catch(MogileFs\Exception $e) {
	$e->getMessage() === 'class_not_found';
}


$fileClient = new MogileFs\Client\FileClient($connection, 'example.com');
//or
$fileClient = new MogileFs\Client\FileClient($connection);
$fileClient->setDomain('example.com');

$fileClient = new MogileFs\Client\FileClient($connection, 'example.com');

try {
	$path = $fileClient->get('test/key');
	$path->getPath(); // first path
	$path->getPaths(); // array of paths
	$path->getCount(); // number of paths
} catch(MogileFs\Exception $e) {
	$e->getMessage() === 'unknown_key';
}

$fileClient = new MogileFs\Client\FileClient($connection, 'example.com');

try {
	$file = $fileClient->info('test/key');
	$file->getFid(); // file id
	$file->getKey(); // file key - test/key
	$file->getSize(); // file size
	$file->getFileCount(); // replication count
	$file->getDomain(); // file domain
	$file->getClass(); // file class
} catch(MogileFs\Exception $e) {
	$e->getMessage() === 'unknown_key';
}

$fileClient = new MogileFs\Client\FileClient($connection, 'example.com');

$response = $fileClient->delete('test/key');
$response->isSuccess();

$fileClient = new MogileFs\Client\FileClient($connection, 'example.com');

$response = $fileClient->rename('test/key', 'test/key2');
$response->isSuccess();

$fileClient = new MogileFs\Client\FileClient($connection, 'example.com');

$collection = $fileClient->listKeys($prefix, $suffix, $limit);
$collection; // contains file keys nothing else

$collection = $fileClient->listFids($fromFid, $toFid);
$collection; // contains File objects with information about all files in range


$class = "blob-class";
$file = new MogileFs\File\BlobFile('raw data', $class)

$class = "local-file-class";
$file = new MogileFs\File\LocalFile('/path/to/file', $class)

$class = "psr7-file-class";

$uploadFile = $request->getUploadedFiles()['file'];
$file = new MogileFs\File\Psr7File($uploadFile, $class)

//or

$stream;// instance of Psr\Http\Message\StreamInterface
$file = new MogileFs\File\Psr7File($stream, $class)

$class = "resource-file-class";
$resource = fopen('file', 'r'); // should work with any stream context
$file = new MogileFs\File\ResourceFile($resource, $class)

$factory = new MogileFs\File\Factory();
$file = $factory($fileContent, $class);

$fileClient = new MogileFs\Client\FileClient($connection, 'example.com');
$factory = new MogileFs\File\Factory();

$key = 'test/key';
$file = $factory($fileContent, $class);
$response = $fileClient->upload($key, $file);
if ($response->isSuccess()) {
	$path = $fileClient->get($key);
}