Skip to content

PHP SDK for Sirv S3

By , Customer Success DirectorUpdated

Open this page in a new chat

Short answer

Use the AWS SDK for PHP with the endpoint s3.sirv.com and your Sirv S3 credentials, or the Laravel package for the Sirv S3 API. The examples check if a file exists, list objects, upload, download, copy, rename and delete files, and create folders. The Sirv REST API is another option.

After reading this you will know

  • Which API to choose for managing Sirv files with PHP
  • How to use the S3 API from PHP or Laravel
  • How to upload, download and manage files with PHP

There are multiple ways to use PHP to manage your files on Sirv.

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 60 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.

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.

  1. Download the latest version of the Sirv API class (zipped PHP file).
  2. Require the Sirv API class:
require_once('sirv.api.class.php');
  1. Initialize the class instance with your Sirv S3 key, secret and bucket, from your Uploads settings.
$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'
);
  1. Check that the connection is working:
if ($sirvClient->testConnection()) {
// Connection SUCCEEDED
} else {
// Connection FAILED
}

Now you can perform the common actions below.

// 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');
$items = $sirvClient->getBucketContents('example/folder');
// 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 the folder example/folder on Sirv
$sirvClient->createFolder('example/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 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 the file example/folder/image.jpg to example/folder/image_copy.jpg
$sirvClient->copyFile('example/folder/image.jpg', 'example/folder/image_copy.jpg');
// Rename the file example/folder/image.jpg to example/folder/image_new.jpg
$sirvClient->renameFile('example/folder/image.jpg', 'example/folder/image_new.jpg');

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

Back to top