1. Go to this page and download the library: Download codebar-ag/laravel-m-files 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/ */
codebar-ag / laravel-m-files example snippets
use CodebarAg\MFiles\Connectors\MFilesConnector;
use CodebarAg\MFiles\DTO\ConfigWithCredentials;
$config = new ConfigWithCredentials(
url: 'https://your-mfiles-server.com',
vaultGuid: '{ABC0DE2G-3HW-QWCQ-SDF3-WERWETWETW}',
username: 'your-username',
password: 'your-password',
cacheDriver: 'file',
tokenTtlSeconds: 3600,
);
$connector = new MFilesConnector(configuration: $config);
use CodebarAg\MFiles\Requests\LogInToVaultRequest;
// Manual authentication (if needed)
$request = new LogInToVaultRequest(
url: 'https://your-mfiles-server.com',
vaultGuid: '{ABC0DE2G-3HW-QWCQ-SDF3-WERWETWETW}',
username: 'your-username',
password: 'your-password',
);
$token = $request->send()->dto();
// Returns authentication token as string
use CodebarAg\MFiles\Requests\LogInToVaultRequest;
$request = new LogInToVaultRequest(
url: 'https://your-mfiles-server.com',
vaultGuid: '{ABC0DE2G-3HW-QWCQ-SDF3-WERWETWETW}',
username: 'your-username',
password: 'your-password',
);
$token = $request->send()->dto();
// Returns authentication token as string
use CodebarAg\MFiles\Requests\UploadFileRequest;
$request = new UploadFileRequest(
fileContent: $fileContent,
fileName: 'document.pdf'
);
$uploadedFile = $connector->send($request)->dto();
// Returns array with file information including Title, Extension, and other metadata
use CodebarAg\MFiles\Requests\CreateSingleFileDocumentRequest;
use CodebarAg\MFiles\DTO\SetProperty;
use CodebarAg\MFiles\Enums\MFDataTypeEnum;
$request = new CreateSingleFileDocumentRequest(
title: 'My Document',
files: [$uploadedFile]
);
use CodebarAg\MFiles\Requests\DownloadFileRequest;
$request = new DownloadFileRequest(
objectType: 0,
objectId: 123,
objectVersion: 1,
fileId: 456
);
use CodebarAg\MFiles\DTO\DownloadedFile;
$downloadedFile = $connector->send($request)->dto();
// Returns DownloadedFile DTO with content, name, extension, size, contentType
use CodebarAg\MFiles\Requests\GetObjectInformationRequest;
$request = new GetObjectInformationRequest(
objectType: 0,
objectId: 123,
objectVersion: 1
);
use CodebarAg\MFiles\DTO\ObjectProperties;
$objectProperties = $connector->send($request)->dto();
// Returns ObjectProperties DTO with object information and properties
use CodebarAg\MFiles\Requests\SetPropertiesRequest;
use CodebarAg\MFiles\DTO\SetProperty;
use CodebarAg\MFiles\Enums\MFDataTypeEnum;
$propertyValues = [
new SetProperty(1856, MFDataTypeEnum::BOOLEAN, true),
new SetProperty(0, MFDataTypeEnum::TEXT, 'Updated Title'),
];
$request = new SetPropertiesRequest(
objectType: 140,
objectId: 1770,
objectVersion: -1,
propertyValues: $propertyValues
);
use CodebarAg\MFiles\DTO\ObjectProperties;
$objectProperties = $connector->send($request)->dto();
// Returns ObjectProperties DTO with updated object information
use CodebarAg\MFiles\Exceptions\MFilesErrorException;
try {
$document = $connector->send($request)->dto();
} catch (MFilesErrorException $e) {
$e->getMessage(); // The message exactly as M-Files reported it
$e->status(); // HTTP status, or 0 when it could not be determined
$e->errorCode(); // M-Files error code, or null when the payload carried none
$e->isAuthenticationFailure(); // true for 401 / 403
$e->context(); // Single-line summary for logs: status, exception name, code, method and URL
$e->error; // The underlying MFilesError DTO (errorCode, status, url, method, exceptionName, exceptionMessage, stack)
}