Search Our Database

How to upload and delete files in buckets using PHP

Last updated on |

This guide will assist you in programmatically uploading and deleting files in a bucket using PHP. Sample code that is compatible with both PHP 8.1 and PHP 8.2 will be included, though the examples in this guide will utilise PHP 8.2. To get started, let’s ensure that we have the necessary dependencies in our environment by running the following commands. Be sure to use the commands that are appropriate for your specific distribution.

# Debian based distributions
sudo apt install software-properties-common
sudo apt update
sudo apt install php82 php-xml unzip wget
wget -O - -q | php -- --quiet
sudo mv composer.phar /usr/local/bin/composer


# Red Hat Enterprised Linux 9 based distributions
sudo dnf install epel-release
sudo dnf install
sudo dnf install dnf-utils
sudo dnf module reset php
sudo dnf module install php:remi-8.2
sudo dnf install php8.2 php-xml unzip wget
wget -O composer-installer.php
sudo php composer-installer.php --filename=composer --install-dir=/usr/local/bin
rm composer-installer.php


Confirm that PHP 8.2 has been installed in your environment.

php -v


You should receive the following output:

PHP 8.2.11 (cli) (built: Oct  6 2023 09:47:18) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.11, Copyright (c) Zend Technologies
with Zend OPcache v8.2.11, Copyright (c), by Zend Technologies


We have provided some sample code that allows you to upload and delete files in a bucket. To download the sample code and install the necessary project dependencies, use the following commands:

tar -xf php-8.tar.gz
cd php-8
composer install



All scripts are located in the


directory which we will set as our working directory:

cd src


Before using the provided scripts to upload and delete files, you will need to configure your access credentials for the bucket and ensure that the endpoint is correctly specified. Open


with a text editor of your choice.

vim adapter.php


Edit lines 7, 8, 9 and 13 in


to include your access key, secret key, endpoint and region.

'endpoint'    => '',
'region'      => 'ap-southeast-mys1',
'credentials' => ['key' => 'ACCESS_KEY', 'secret' => 'SECRET_KEY'],
return new League\Flysystem\AwsS3V3\AwsS3V3Adapter($client, 'sample-bucket-01');


If you’re unsure about the endpoint of your bucket, access the customer portal and check the object storage tab. In our example, we have a bucket named “sample-bucket-01” which has “” as an endpoint.


Configure your endpoint and region accordingly:

'endpoint': '',
'region': 'ap-southeast-mys1'


Uploading files

To upload files, Create a directory to store assets that you will upload to the bucket. In this example, we create a directory named


and a sample file called “sample_upload.txt” which we will be loading.

mkdir assets
echo "Hello world" > assets/sample_upload.txt


Now, configure the sample code in



vim uploadFile.php


Edit line 16 to specify the name of the file after it is uploaded to the bucket, and the path to the file to be uploaded. For example:

$filesystem->write('uploaded_text.txt', file_get_contents('./assets/sample_upload.txt'));




configured, run the script using:

php uploadFile.php


If you do not receive any output after running the script, your file has been successfully uploaded to your bucket.


Deleting files

To delete a file from the bucket, configure



vim deleteFile.php


Edit line 15 to specify the bucket and the file to be deleted. For example:





configured, run the script using:

php deleteFile.php


If you do not receive any output after running the script, your file has been successfully deleted from your bucket.


For a guide on how to programmatically upload and delete files from buckets using JavaScript, refer to the following guide.