PHP code example of files.com / files-php-sdk
1. Go to this page and download the library: Download files.com/files-php-sdk 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/ */
files.com / files-php-sdk example snippets title="Example Request"
\Files\Files::setApiKey('YOUR_API_KEY');
## Alternatively, you can specify the API key on a per-object basis in the second parameter to a model constructor.
$user = new \Files\Model\User($params, array('api_key' => 'YOUR_API_KEY'));
## You may also specify the API key on a per-request basis in the final parameter to static methods.
\Files\Model\User::find($id, $params, array('api_key' => 'YOUR_API_KEY'));
title="Example Request"
$session = \Files\Model\Session::create(['username' => 'motor', 'password' => 'vroom']);
title="Example Request"
## You may set the returned session ID to be used by default for subsequent requests.
\Files\Files::setSessionId($session->id);
## Alternatively, you can specify the session ID on a per-object basis in the second parameter to a model constructor.
$user = new \Files\Model\User($params, array('session_id' => $session->id));
## You may also specify the session ID on a per-request basis in the final parameter to static methods.
\Files\Model\User::find($id, $params, array('session_id' => $session->id));
title="Example Request"
\Files\Model\Session::destroy();
title="Example setting"
\Files\Files::$autoPaginate = false
title="Example setting"
\Files\Files::setBaseUrl('https://MY-SUBDOMAIN.files.com');
title="Example setting"
\Files\Files::$logLevel = \Files\LogLevel::DEBUG
title="Example setting"
\Files\Files::$debugRequest = true
title="Example setting"
\Files\Files::$debugResponseHeaders = true
title="Example setting"
\Files\Files::$connectTimeout = 20.0
title="Example setting"
\Files\Files::$readTimeout = 60
title="Example setting"
\Files\Files::$minNetworkRetryDelay = 1.0
title="Example setting"
\Files\Files::$maxNetworkRetryDelay = 3.0
title="Example setting"
\Files\Files::$maxNetworkRetries = 5
title="Sort Example"
// users sorted by username
\Files\Files::setApiKey('my-key');
$users = \Files\Model\User::list(array(
'sort_by' => array("username" => "asc")
));
title="Exact Filter Example"
// non admin users
\Files\Files::setApiKey('my-key');
$users = \Files\Model\User::list(array(
'filter' => array("not_site_admin" => true)
));
foreach ($users as $value) {
print("User username: " . $value->getUserName() . "\n");
}
title="Range Filter Example"
// users who haven't logged in since 2024-01-01
\Files\Files::setApiKey('my-key');
$users = \Files\Model\User::list(array(
'filter_gteq' => array("last_login_at" => "2024-01-01")
));
foreach ($users as $value) {
print("User username: " . $value->getUserName() . "\n");
}
title="Pattern Filter Example"
// users whose usernames start with 'test'
\Files\Files::setApiKey('my-key');
$users = \Files\Model\User::list(array(
'filter_prefix' => array("username" => "test")
));
foreach ($users as $value) {
print("User username: " . $value->getUserName() . "\n");
}
title="Combination Filter with Sort Example"
// users whose usernames start with 'test' and are not admins
\Files\Files::setApiKey('my-key');
$users = \Files\Model\User::list(array(
'filter_prefix' => array("username" => "test"),
'filter' => array("not_site_admin" => true),
'sort_by' => array("last_login_at" => "asc")
));
foreach ($users as $value) {
print("User username: " . $value->getUserName() . "\n");
}
title="Example Error Handling"
try {
$session = Files\Model\Session::create(['username' => 'USERNAME', 'password' => 'BADPASSWORD']);
} catch (Files\NotAuthenticated\InvalidUsernameOrPasswordException $e) {
echo 'Authentication Error Occurred (' . get_class($e) . '): ', $e->getMessage(), "\n";
} catch (Files\FilesException $e) {
echo 'Unknown Error Occurred (' . get_class($e) . '): ', $e->getMessage(), "\n";
}
$rootFiles = \Files\Model\Folder::listFor('/');
foreach ($rootFiles as $file) {
echo $file->getPath() . "\n";
}
$sourceFilePath = 'local.txt';
$destinationFileName = 'uploads/remote.txt';
\Files\Model\File::uploadFile($destinationFileName, $sourceFilePath);
\Files\Model\File::uploadFile($destinationFileName, $sourceFilePath, ['mkdir_parents' => true]);
$fileData = 'contents';
$destinationFileName = 'uploads/remote.txt';
\Files\Model\File::uploadData($destinationFileName, $fileData);
$outputStream = fopen('php://output', 'w');
$remoteFilePath = 'uploads/remote.txt';
\Files\Model\File::downloadToStream($remoteFilePath, $outputStream);
$localFilePath = 'local.txt';
$remoteFilePath = 'uploads/remote.txt';
// download entire file - with retries enabled
\Files\Model\File::downloadToFile($remoteFilePath, $localFilePath);
// partially download - just the first KB
\Files\Model\File::partialDownloadToFile($remoteFilePath, $localFilePath, 0, 1023);
// resume an incomplete download
\Files\Model\File::resumeDownloadToFile($remoteFilePath, $localFilePath);
$remoteFilePath = 'uploads/remote.txt';
$foundFile = \Files\Model\File::find($remoteFilePath);
$remoteFilePath = 'uploads/remote.txt';
$file = new \Files\Model\File();
$file->get($remoteFilePath);
$file->update([
'provided_mtime' => '2000-01-01T01:00:00Z',
'priority_color' => 'red',
]);
$file->metadata([
'with_previews' => true,
'with_priority_color' => true,
]);
if(\Files\Util\PathUtil::same("Fïłèńämê.Txt", "filename.txt")) {
echo "Paths are the same\n";
}
shell
curl -sS https://getcomposer.org/installer | php
shell
php composer.phar