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:

    require_once('sirv.api.class.php');
  3. 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'
    );
  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)
$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');

Was this article helpful?

Related articles

Get help from a Sirv expert