PHP SDK for Sirv S3
There are multiple ways to use PHP to manage your files on Sirv.
Choose an API
Before you start, choose which API you will use.
Sirv provides two APIs for managing your files:
- REST API - we recommend using the Sirv REST API. It has more than 40 API methods, it is designed and support by the Sirv team.
- S3 API - the S3 API has fewer API methods and requires some more effort to integrate with but AWS SDKs can be used.
This page describes how to use the S3 API for PHP.
S3 API for PHP
If you're using PHP, you can manage your files with the AWS SDK for PHP.
Alternatively, you can use the simplified class below, which provides a basic API for working with Sirv storage.
- Download the latest version of the Sirv API class (zipped PHP file).
-
Require the Sirv API class:
require_once('sirv.api.class.php');
-
Initialize the class instance with your Sirv S3 key, secret and bucket, from your Settings page.
$sirvClient = new SirvClient( // S3 bucket 'ENTER_YOUR_SIRV_S3_BUCKET_HERE', // S3 Access Key 'ENTER_YOUR_SIRV_S3_KEY_HERE', // S3 Secret Key 'ENTER_YOUR_SIRV_S3_SECRET_HERE' );
- Check that the connection is working:
if ($sirvClient->testConnection()) { // Connection SUCCEEDED } else { // Connection FAILED }
Now you can perform the common actions below.
Check if file/folder exists
// Check if a folder exists (example/folder) $sirvClient->checkIfObjectExists('example/folder'); // Check if a file exists (example/folder/image.jpg) $sirvClient->checkIfObjectExists('example/folder','image.jpg');
Get object list
$items = $sirvClient->getBucketContents('example/folder');
Upload files to Sirv
// Upload the local file local/folder/image.jpg to the folder example/folder/ on Sirv $res = $sirvClient->uploadFile( // File path on Sirv 'example/folder/image.jpg', // Local file name 'local/folder/image.jpg' );
Create a folder
// Create the folder example/folder on Sirv $sirvClient->createFolder('example/folder');
Delete a file or folder
All files must be deleted from a folder before a folder can be deleted.
// Delete the file example/folder/image.jpg from Sirv $sirvClient->deleteFile('example/folder/image.jpg');
Download a file
// Download the file example/folder/image.jpg from Sirv $fileContents = $sirvClient->getFile('example/folder/image.jpg'); if (!empty($fileContents)) { file_put_contents('image.jpg', $fileContents); }
Copy a file
// Copy the file example/folder/image.jpg to example/folder/image_copy.jpg $sirvClient->copyFile('example/folder/image.jpg', 'example/folder/image_copy.jpg');
Rename a file
// Rename the file example/folder/image.jpg to example/folder/image_new.jpg $sirvClient->renameFile('example/folder/image.jpg', 'example/folder/image_new.jpg');
Laravel package for Sirv S3 API
If you use Laravel, consider using this Laravel wrapper to speed up integration.
This Laravel filesystem driver for Sirv files was created by a member of the Sirv community.
GitHub package and tutorial: https://github.com/lipit146/filesystem-sirv
Packagist repository: https://packagist.org/packages/lipit146/filesystem-sirv