Cloudinary is a powerful cloud-based media management platform that allows you to upload, store, manage, manipulate, and deliver images and videos for your websites and apps. In this guide, I’ll walk you through the process of integrating Cloudinary with your PHP application.
First, you’ll need to install the official Cloudinary PHP SDK using Composer.
composer require cloudinary/cloudinary_php
This will install the Cloudinary PHP SDK and all its dependencies in your project’s vendor
directory.
After installing the SDK, you need to configure it with your Cloudinary account credentials.
Important: Keep your API secret secure and never expose it in client-side code.
cloudinary_config.php
) in your project:<?php
require 'vendor/autoload.php';
use Cloudinary\Configuration\Configuration;
use Cloudinary\Api\Upload\UploadApi;
// Configure Cloudinary
Configuration::instance([
'cloud' => [
'cloud_name' => 'your_cloud_name',
'api_key' => 'your_api_key',
'api_secret' => 'your_api_secret'
],
'url' => [
'secure' => true
]
]);
Replace 'your_cloud_name'
, 'your_api_key'
, and 'your_api_secret'
with your actual Cloudinary credentials.
Now, let’s create a simple HTML form to upload images to Cloudinary.
upload_form.php
:<!DOCTYPE html>
<html>
<head>
<title>Cloudinary PHP Upload</title>
</head>
<body>
<h1>Upload Image to Cloudinary</h1>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" accept="image/*" required>
<button type="submit">Upload</button>
</form>
</body>
</html>
Next, create the PHP script that will handle the file upload to Cloudinary.
upload.php
:<?php
require 'cloudinary_config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
try {
// Get the temporary file path
$filePath = $_FILES['image']['tmp_name'];
// Upload the image to Cloudinary
$upload = (new UploadApi())->upload($filePath, [
'folder' => 'php_uploads/',
'public_id' => 'img_' . time(),
'overwrite' => true,
'resource_type' => 'image'
]);
// Display the uploaded image
echo "<h1>Upload Successful!</h1>";
echo "<p>Public ID: " . htmlspecialchars($upload['public_id']) . "</p>";
echo "<p>Secure URL: " . htmlspecialchars($upload['secure_url']) . "</p>";
echo "<img src='" . htmlspecialchars($upload['secure_url']) . "' width='500'>";
} catch (Exception $e) {
echo "<h1>Upload Failed</h1>";
echo "<p>" . htmlspecialchars($e->getMessage()) . "</p>";
}
} else {
header('Location: upload_form.php');
exit;
}
php -S localhost:8000
http://localhost:8000/upload_form.php
Cloudinary allows you to apply transformations to your images on the fly. Here’s how to generate a transformed image URL:
$transformedUrl = cloudinary_url($upload['public_id'], [
'width' => 300,
'height' => 200,
'crop' => 'fill',
'effect' => 'sepia',
'quality' => 'auto'
]);
echo "<img src='" . htmlspecialchars($transformedUrl) . "'>";
To delete an uploaded image:
<?php
require 'cloudinary_config.php';
$publicId = 'php_uploads/img_1234567890'; // Replace with your actual public ID
try {
$result = (new UploadApi())->destroy($publicId);
echo "Image deleted successfully: " . print_r($result, true);
} catch (Exception $e) {
echo "Error deleting image: " . $e->getMessage();
}
To list all uploaded images in a specific folder:
<?php
require 'cloudinary_config.php';
try {
$resources = (new AdminApi())->assets([
'type' => 'upload',
'prefix' => 'php_uploads/',
'max_results' => 10
]);
echo "<h1>Uploaded Images</h1>";
foreach ($resources['resources'] as $resource) {
echo "<img src='" . htmlspecialchars($resource['secure_url']) . "' width='200'><br>";
echo "Public ID: " . htmlspecialchars($resource['public_id']) . "<br><br>";
}
} catch (Exception $e) {
echo "Error listing resources: " . $e->getMessage();
}
You’ve now successfully integrated Cloudinary with your PHP application! This gives you powerful media management capabilities including:
From here, you can explore more advanced features like:
Remember to check the official Cloudinary PHP SDK documentation for more detailed information and advanced usage.
In a surprising turn of events, former President Donald Trump announced on June 19, 2025,…
Flexbox is a powerful layout system in React Native that allows developers to create responsive…
"The journey of a thousand miles begins with a single step." — Lao Tzu Welcome…
We often describe ourselves as "processing" information, "rebooting" after a bad day, or feeling "overloaded"…
QR codes have evolved from a niche tracking technology to an indispensable digital connector, seamlessly…
Artificial Intelligence (AI) has made remarkable progress in recent years, transforming industries such as healthcare,…