Search Our Database
How to upload and delete files in buckets using PHP
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.
sudo apt install software-properties-common
sudo apt update
sudo apt install php82 php-xml unzip wget
wget https://raw.githubusercontent.com/composer/getcomposer.org/76a7060ccb93902cd7576b67264ad91c8a2700e2/web/installer -O - -q | php -- --quiet
sudo mv composer.phar /usr/local/bin/composer
sudo dnf install epel-release
sudo dnf install http://rpms.remirepo.net/enterprise/remi-release-9.rpm
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 https://getcomposer.org/installer -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.
You should receive the following output:
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
Pre-configurations
All scripts are located in the
directory which we will set as our working directory:
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.
Edit lines 7, 8, 9 and 13 in
to include your access key, secret key, endpoint and region.
'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 “ap-southeast-mys1.oss.ips1cloud.com” as an endpoint.
Configure your endpoint and region accordingly:
'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.
echo "Hello world" > assets/sample_upload.txt
Now, configure the sample code in
.
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:
With
configured, run the script using:
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
.
Edit line 15 to specify the bucket and the file to be deleted. For example:
With
configured, run the script using:
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.