Categories: Development

Creating folders, renaming and deleting with BunnyCDN PHP API class

Following on from this post, I use the BunnyCDN PHP API class to create folders, delete and rename files in a Storage zone.

Setting my imageBackups Storage zone user and password:

$bunny->zoneConnect('imagebackups', 'PASSWORD-HERE');

I can now interact with the Storage zone using the BunnyCDN API PHP class.

Creating folders

Creating 2 new folders in the root directory of imageBackups is done with:

$bunny->createFolder('Camera');
$bunny->createFolder('Phone');
$bunny->jsonHeader();
echo $bunny->listAll('');

Returns

{
  "storage_name": "imagebackups",
  "current_dir": "",
  "data": [
    {
      "name": "adroneimage.jpg",
      "file_type": "jpg",
      "size": 876.76171875,
      "is_dir": false,
      "created": "2019-11-26 04:42:17",
      "last_changed": "2019-11-26 04:42:17",
      "guid": "09ab5cbb-2e97-4390-8782-05f7018cf9da"
    },
    {
      "name": "Camera",
      "file_type": null,
      "size": null,
      "is_dir": true,
      "created": "2019-11-28 05:20:16",
      "last_changed": "2019-11-28 05:20:16",
      "guid": "98d0b5b3-8971-4f17-a1b7-e2d5774aec01"
    },
    {
      "name": "Drone",
      "file_type": null,
      "size": null,
      "is_dir": true,
      "created": "2019-11-26 04:30:20",
      "last_changed": "2019-11-26 04:30:20",
      "guid": "906cde44-aaa5-464d-8d92-3a019c1a2854"
    },
    {
      "name": "Phone",
      "file_type": null,
      "size": null,
      "is_dir": true,
      "created": "2019-11-28 05:20:17",
      "last_changed": "2019-11-28 05:20:17",
      "guid": "0544ea46-dcd1-4f91-b836-a1f7bf4d42b5"
    }
  ]
}

This data shows details about objects and amongst this what is a directory or file.

Deleting a file

Let’s delete adroneimage.jpg by doing

echo $bunny->deleteFile('adroneimage.jpg');

From this previous post I uploaded some images into Drone/theLake I can check this by doing:

echo $bunny->listAll('/Drone/theLake');

Which returns

{
  "storage_name": "imagebackups",
  "current_dir": "/Drone/theLake",
  "data": [
    {
      "name": "adroneimage.jpg",
      "file_type": "jpg",
      "size": 876.76171875,
      "is_dir": false,
      "created": "2019-11-26 04:43:51",
      "last_changed": "2019-11-26 04:43:51",
      "guid": "a8d8a1f0-5915-4cf2-b538-a305fe4d440e"
    },
    {
      "name": "tullaroop-reservoir-drone-15.jpg",
      "file_type": "jpg",
      "size": 984.2802734375,
      "is_dir": false,
      "created": "2019-11-26 04:53:38",
      "last_changed": "2019-11-26 04:53:38",
      "guid": "c559636e-7d14-4ff5-97a7-e9cc6247e5cd"
    },
    {
      "name": "tullaroop-reservoir-drone-16.jpg",
      "file_type": "jpg",
      "size": 1118.3857421875,
      "is_dir": false,
      "created": "2019-11-26 04:53:49",
      "last_changed": "2019-11-26 04:53:49",
      "guid": "981b68b3-4b53-4717-aa3a-78b714cf2481"
    }
  ]
}

Getting the overall size of this directory:

echo $bunny->dirSize('/Drone/theLake');

Returns:

{
  "dir": "/Drone/theLake",
  "files": 3,
  "size_b": 3050934,
  "size_kb": "2,979.428",
  "size_mb": "2.910",
  "size_gb": "0.003"
}

Rename

I want to rename adroneimage.jpg to tullaroop-reservoir-drone-14.jpg I can do that with:

echo $bunny->rename('Drone/theLake/adroneimage.jpg', 'Drone/theLake/tullaroop-reservoir-drone-14.jpg');

Now the file is renamed.

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago