PHP SDK for Sirv S3

On this page

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:

  3. Initialize the class instance with your Sirv S3 key, secret and bucket, from your Settings page.

    $sirvClient = new SirvClient(
      // S3 bucket
      // S3 Access Key
      // S3 Secret Key
  4. 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)

// Check if a file exists (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
  // Local file name

Create a folder

// Create the folder example/folder on Sirv

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

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');

Was this article helpful?

Get help from a Sirv expert

help ukraine help ukraine Powered by Ukrainian determination and British ingenuity

How can you support Ukraine