Search Our Database
How to upload and delete files in buckets using PHP
Introduction
This guide will help you programmatically upload and delete files in a storage bucket using PHP, specifically with sample code compatible with both PHP 8.1 and PHP 8.2 (examples in this guide use PHP 8.2). This tutorial covers the installation of PHP, setting up necessary dependencies, and running scripts to upload and delete files in your bucket. The guide is intended for Debian-based and Red Hat-based distributions, so be sure to follow the appropriate installation steps for your environment.
Prerequisites
- PHP 8.2 installed on your server.
- Composer for managing PHP dependencies.
- Access credentials for your bucket (access key, secret key, endpoint, and region).
- Basic knowledge of command-line usage and editing configuration files.
Step-by-Step Guide
Step 1: Install PHP 8.2 and Composer
For Debian-based Distributions
Update the system and install PHP with required extensions:
Download and install Composer:
For Red Hat Enterprise Linux 9-based Distributions
Install PHP 8.2 with the necessary modules:
Download and install Composer:
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
Step 2: Confirm PHP Installation
Verify that PHP 8.2 is installed correctly by checking the PHP version:
You should see output similar to the following:
Step 3: Download Sample Code and Install Dependencies
Download the provided sample code for uploading and deleting files:
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
Step 4: Set Up Working Directory
Navigate to the src directory where the scripts are located:
cd src
Step 5: Configure Access Credentials and Endpoint
Open adapter.php in a text editor to set up your access credentials and endpoint information:
vim adapter.php
Edit the following lines to add your bucket credentials and region details:
Configure your endpoint and region accordingly:
'endpoint': 'https://ap-southeast-mys1.oss.ips1cloud.com', 'region': 'ap-southeast-mys1'
Uploading Files
Step 1: Prepare Files for Upload
Create a directory for the assets you wish to upload and create a sample file:
mkdir assets echo "Hello world" > assets/sample_upload.txt
Step 2: Configure uploadFile.php
Open uploadFile.php in a text editor:
vim uploadFile.php
Specify the name of the file after it is uploaded to the bucket and the path of the file to be uploaded. Edit line 16 as shown below:
$filesystem->write('uploaded_text.txt', file_get_contents('./assets/sample_upload.txt'));
Step 3: Run the Upload Script
Run the upload script to transfer the file to your bucket:
php uploadFile.php
If the upload is successful, no output will be displayed.
Deleting Files
Step 9: Configure deleteFile.php
Open deleteFile.php in a text editor:
Edit line 15 to specify the file you want to delete from the bucket:
Step 10: Run the Delete Script
Run the delete script to remove the specified file from your bucket:
If the deletion is successful, there will be no output displayed.
Conclusion
This guide has shown you how to programmatically upload and delete files in a storage bucket using PHP. Configuring adapter.php with the correct credentials and endpoint details ensures secure and efficient file management in the bucket. For a guide on how to programmatically upload and delete files from buckets using JavaScript, refer to the following guide.
Should you have any inquiries about the guidelines, please feel free to open a ticket through your portal account or contact us at support@ipserverone.com. We’ll be happy to assist you further.
Article posted on 15 October 2023 by Louis