Current Location: Home> Latest Articles> How to Upload Base64 Images to Qiniu Cloud Storage Using PHP?

How to Upload Base64 Images to Qiniu Cloud Storage Using PHP?

M66 2025-06-18

How to Upload Base64 Images to Qiniu Cloud Storage Using PHP?

Qiniu Cloud Storage is a powerful cloud storage platform that provides developers with a rich set of APIs and tools for file storage and management. When using Qiniu Cloud, you might come across the need to upload images in Base64 format. This article will walk you through how to achieve this with PHP.

Step 1: Preparations

Before we start, there are a few things you need to prepare:

  1. Sign up for a Qiniu Cloud account and create a storage bucket.
  2. Install the Composer package manager.
  3. Obtain your Qiniu Cloud Access Key and Secret Key.

Step 2: Install Required Dependencies

Use Composer to install Qiniu Cloud's PHP SDK:

composer require qiniu/php-sdk

Once the installation is complete, you can start writing the code to handle the image upload.

Step 3: PHP Code Example

Here’s an example of how to upload a Base64 image to Qiniu Cloud Storage using PHP:


<?php
require __DIR__ . '/vendor/autoload.php'; // Include Composer's autoload file

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

// Qiniu Cloud Access Key and Secret Key
$accessKey = 'your-access-key';
$secretKey = 'your-secret-key';

// The bucket name to upload to
$bucket = 'your-bucket-name';

// Create an Auth object
$auth = new Auth($accessKey, $secretKey);

// Generate the upload token
$token = $auth->uploadToken($bucket);

// File name after upload to Qiniu
$fileName = 'your-upload-filename'; // You can customize the file name

// Base64 encoded image data
$base64Image = 'your-base64-image-data';

// Convert Base64 data to file stream
$stream = base64_decode($base64Image);

// Initialize UploadManager object and upload
$uploadMgr = new UploadManager();
list($ret, $err) = $uploadMgr->put($token, $fileName, $stream);

if ($err !== null) {
    // Upload failed
    echo 'Upload failed: ' . $err->message();
} else {
    // Upload successful
    echo 'Upload successful';
    // File information returned
    var_dump($ret);
}
?>

Code Explanation

  1. Include the autoload file: First, include the Composer autoload file to load the required libraries.

  2. Configure Qiniu Cloud account info: Set the Access Key, Secret Key, and the bucket name.

  3. Generate the upload token: Use the Auth class to generate the upload token.

  4. Handle the Base64 image: Convert the Base64 encoded image data into a file stream.

  5. Upload the image: Use the UploadManager object to upload the file stream to Qiniu Cloud Storage.

Step 4: Upload Result

After a successful upload, you will receive file information. You can use this information to process the file further, such as generating an access link or storing the file ID. If the upload fails, an error message will be displayed.

Conclusion

By using PHP and Qiniu Cloud's SDK, you can easily upload Base64 encoded images to Qiniu Cloud Storage. This method allows you to not only upload images but also manage them in the cloud, increasing your development efficiency.