> ## Documentation Index
> Fetch the complete documentation index at: https://bunnynet-cb9733c2-support-migration.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP SDK

> Official PHP SDK for Bunny Storage

The official PHP SDK provides an easy way to interact with Bunny Storage in your PHP applications.

## Installation

```bash theme={null}
composer require bunnycdn/storage
```

## Quickstart

### Connect to your storage zone

```php theme={null}
use Bunny\Storage\Client;
use Bunny\Storage\Region;

$client = new Client(
    'your-access-key',
    'your-storage-zone-name',
    Region::FALKENSTEIN // Optional: storage zone region
);
```

**Available regions:**

| Region Constant        | Code  | Location         | City                  |
| ---------------------- | ----- | ---------------- | --------------------- |
| `Region::FALKENSTEIN`  | `de`  | Frankfurt, DE    | Falkenstein (default) |
| `Region::LONDON`       | `uk`  | London, UK       | London                |
| `Region::STOCKHOLM`    | `se`  | Stockholm, SE    | Stockholm             |
| `Region::NEW_YORK`     | `ny`  | New York, US     | New York              |
| `Region::LOS_ANGELES`  | `la`  | Los Angeles, US  | Los Angeles           |
| `Region::SINGAPORE`    | `sg`  | Singapore, SG    | Singapore             |
| `Region::SYDNEY`       | `syd` | Sydney, AU       | Sydney                |
| `Region::SAO_PAULO`    | `br`  | Sao Paulo, BR    | Sao Paulo             |
| `Region::JOHANNESBURG` | `jh`  | Johannesburg, ZA | Johannesburg          |

### List files

```php theme={null}
$files = $client->listFiles('remote/path/');

// Navigate subdirectories
$subfolderFiles = $client->listFiles('my-folder/');
```

Returns an array of `FileInfo` objects.

**FileInfo properties:**

* `getGuid()` - Unique identifier
* `getName()` - File name
* `getPath()` - Directory path
* `getSize()` - File size in bytes
* `getChecksum()` - File checksum
* `getDateCreated()` - Creation date
* `getDateModified()` - Last modification date
* `isDirectory()` - Whether it's a directory

### Upload a file

<Tabs>
  <Tab title="Single file">
    ```php theme={null}
    $client->upload('/path/to/local/file.txt', 'remote/path/hello-world.txt');
    ```
  </Tab>

  <Tab title="Without checksum">
    ```php theme={null}
    $client->upload('/path/to/local/file.txt', 'remote/path/hello-world.txt', false);
    ```
  </Tab>

  <Tab title="Async">
    ```php theme={null}
    $promise = $client->uploadAsync('/path/to/local/file.txt', 'remote/path/hello-world.txt');
    // Returns a GuzzleHttp\Promise\PromiseInterface
    ```
  </Tab>

  <Tab title="String content">
    ```php theme={null}
    $content = 'Hello, world!';
    $client->putContents('hello-world.txt', $content);

    // Without checksum
    $client->putContents('hello-world.txt', $content, false);
    ```
  </Tab>
</Tabs>

### Upload multiple files

<Tabs>
  <Tab title="Batch (sequential)">
    ```php theme={null}
    $files = glob('./uploads/*');

    foreach ($files as $file) {
        if (is_file($file)) {
            $client->upload($file, 'batch/' . basename($file));
        }
    }
    ```
  </Tab>

  <Tab title="Async (concurrent)">
    ```php theme={null}
    use GuzzleHttp\Promise\Utils;

    $files = [
        '/path/to/file1.jpg' => 'uploads/file1.jpg',
        '/path/to/file2.jpg' => 'uploads/file2.jpg',
        '/path/to/file3.jpg' => 'uploads/file3.jpg',
    ];

    $promises = [];
    foreach ($files as $localPath => $remotePath) {
        $promises[$remotePath] = $client->uploadAsync($localPath, $remotePath);
    }

    $results = Utils::settle($promises)->wait();
    ```
  </Tab>
</Tabs>

### Download a file

<Tabs>
  <Tab title="To local file">
    ```php theme={null}
    $client->download('remote/path/hello-world.txt', '/path/to/local/file.txt');
    ```
  </Tab>

  <Tab title="As string">
    ```php theme={null}
    $content = $client->getContents('hello-world.txt');
    echo $content; // Hello, world!
    ```
  </Tab>
</Tabs>

### Get file info

```php theme={null}
$fileInfo = $client->info('remote/path/hello-world.txt');

echo $fileInfo->getName();
echo $fileInfo->getSize();
echo $fileInfo->getChecksum();
```

### Check if file exists

```php theme={null}
if ($client->exists('remote/path/hello-world.txt')) {
    echo 'File exists!';
}
```

### Delete a file

```php theme={null}
$client->delete('remote/path/hello-world.txt');

// Delete a directory
$client->delete('remote/path/folder/');
```

### Delete multiple files

```php theme={null}
$errors = $client->deleteMultiple(['file1.txt', 'file2.txt', 'non-existing.txt']);

// Returns array of errors for failed deletions
// ['non-existing.txt' => 'Object not found']
```

## Examples

Explore complete working examples in the GitHub repository:

| Example                                                                                                              | Description                                       |
| -------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------- |
| [file-upload](https://github.com/BunnyWay/BunnyCDN.PHP.Storage/tree/master/examples/file-upload)                     | Upload a single file to storage                   |
| [batch-upload](https://github.com/BunnyWay/BunnyCDN.PHP.Storage/tree/master/examples/batch-upload)                   | Upload multiple files sequentially                |
| [async-upload](https://github.com/BunnyWay/BunnyCDN.PHP.Storage/tree/master/examples/async-upload)                   | Upload multiple files concurrently using promises |
| [form-upload](https://github.com/BunnyWay/BunnyCDN.PHP.Storage/tree/master/examples/form-upload)                     | Handle file uploads from HTML forms               |
| [list-files](https://github.com/BunnyWay/BunnyCDN.PHP.Storage/tree/master/examples/list-files)                       | List all files in a directory                     |
| [download-file](https://github.com/BunnyWay/BunnyCDN.PHP.Storage/tree/master/examples/download-file)                 | Download a file from storage                      |
| [file-info](https://github.com/BunnyWay/BunnyCDN.PHP.Storage/tree/master/examples/file-info)                         | Retrieve file metadata                            |
| [delete-file](https://github.com/BunnyWay/BunnyCDN.PHP.Storage/tree/master/examples/delete-file)                     | Delete a single file                              |
| [delete-multiple-files](https://github.com/BunnyWay/BunnyCDN.PHP.Storage/tree/master/examples/delete-multiple-files) | Delete multiple files in one operation            |
| [delete-old-files](https://github.com/BunnyWay/BunnyCDN.PHP.Storage/tree/master/examples/delete-old-files)           | Find and delete files older than a specified age  |
| [kitchen-sink](https://github.com/BunnyWay/BunnyCDN.PHP.Storage/tree/master/examples/kitchen-sink)                   | Complete UI with upload, list, view, and delete   |

## Resources

* [GitHub Repository](https://github.com/BunnyWay/BunnyCDN.PHP.Storage)
* [Packagist](https://packagist.org/packages/bunnycdn/storage)
