Search Our Database

How to upload and delete files in buckets using PHP

Last updated on |
by

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 https://raw.githubusercontent.com/composer/getcomposer.org/76a7060ccb93902cd7576b67264ad91c8a2700e2/web/installer -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 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.

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:

wget https://www.ipserverone.info/wp-content/uploads/2023/10/php-8.tar.gz
tar -xf php-8.tar.gz
cd php-8
composer install

 

Pre-configurations

All scripts are located in the src 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 adapter.php with a text editor of your choice.

vim adapter.php

 

Edit lines 7, 8, 9 and 13 in adapter.php to include your access key, secret key, endpoint and region.

'endpoint' => 'https://ap-southeast-mys1.oss.ips1cloud.com',
'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:

'endpoint': 'https://ap-southeast-mys1.oss.ips1cloud.com',
'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 assets/ 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 uploadFile.php.

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'));

 

With uploadFile.php 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 deleteFile.php.

vim deleteFile.php

 

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

$filesystem->delete('uploaded_text.txt');

 

With deleteFile.php 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.