Cloudinary Blog

Uploading PHP Files and Rich Media the Easy Way

By Prosper Otemuyiwa
Uploading PHP Files and Rich Media the Easy Way

Rich media is surplus and abundant on the internet. Tons of apps these days deal with rich media. Users spend more time uploading and viewing media content than any other thing.

Cloudinary’s PHP SDK, with which you can easily and speedily upload multiple images and videos in PHP files can help you ramp up quickly while dealing the mechanics of php file uploads and rich media during app development.

This article guides you through the process of uploading PHP files and rich media the easy way.

PHP File Uploads With Cloudinary

The Cloudinary Open-source PHP library utilizes the Cloudinary API which allows for the transmission of any kind of file for safe storage in the cloud. They contain easy-to-use helper methods for these image-related tasks:

  • Uploads
  • Administration and creation of sprites
  • Embeddings
  • Generation of URLs for transformations

To get started, do the following:

Step 1. Sign up for a Cloudinary account.

Sign up for Cloudinary’s generous free-tier plan.

Dashboard with upload options Cloudinary Dashboard: Your cloud name, API key, and API secret are the key values for interacting with Cloudinary’s capabilities.

Step 2. Install the Cloudinary PHP library.

Fetch the PHP SDK Library

Fetch the library with Composer. Alternatively, go to Cloudinary’s PHP repository on GitHub, copy all the required files and paste them in your app. Afterwards, then reference them in the script with which to perform the upload, as follows:

Copy to clipboard
/**
** UPLOADING PHP FILES
***
// importing the necessary Cloudinary files
require 'Cloudinary.php';
require 'Uploader.php';
require 'Helpers.php';
require 'Api.php';
.....

To manage your PHP library’s dependency with Composer, install Cloudinary's PHP library directly from the Packagist repository and update your composer.json file, as follows:

Copy to clipboard
{
  "require": {
    "cloudinary/cloudinary_php": "dev-master"
  }
}

Now install the dependencies, including Cloudinary's PHP package:

Copy to clipboard
php composer.phar install

For more details, see Cloudinary’s documentation on the PHP SDK.

Step 3. PHP File & Rich Media Uploads to Cloudinary.

You can upload images and other files to Cloudinary in PHP on a server that runs PHP 5.3 or later. Do the following:

First, set up the key values with the config method so that Cloudinary can verify that your account is valid:

Copy to clipboard
\Cloudinary::config(array(
    "cloud_name" => "my_cloud_name",
    "api_key" => "my_api_key",
    "api_secret" => "my_api_secret"
));

Note
For security, load those three values from an environment (.env) file. Alternatively, put them in a file called settings.php and include it in the script that performs the actual uploading.

Now upload your images and other files to Cloudinary’s cloud platform:

  • To upload a local file, e.g.,my_image.jpg, type:

    Copy to clipboard
    \Cloudinary\Uploader::upload("/home/my_image.jpg")

    The upload method then returns an associative array whose content is similar to this snippet:

    Copy to clipboard
    Array
    (
    [public_id] => sample
    [version] => 1312461204
    [width] => 864
    [height] => 576
    [format] => jpg
    [bytes] => 120253
    [url] => https://res.cloudinary.com/demo/image/upload/v1371281596/sample.jpg
    [secure_url] => https://res.cloudinary.com/demo/image/upload/v1371281596/sample.jpg
    )
  • To upload a file from a remote HTTP or HTTPS URL, type:

    Copy to clipboard
    \Cloudinary\Uploader::upload("https://www.example.com/image.jpg")
  • To upload a file from an S3 bucket, type:

    Copy to clipboard
    \Cloudinary\Uploader::upload('s3://my-bucket/my-path/my-file.jpg');

    Cloudinary assigns a public ID to each and every uploaded file for transformation and delivery later on.

    Here’s the syntax for uploading files with PHP to Cloudinary:

    Copy to clipboard
    public static function upload($file, $options = array())

    See these examples of what you can pass to the $options argument:

  • A custom public ID:

    Copy to clipboard
    \Cloudinary\Uploader::upload('my_image.jpg', array("public_id" => "manutd_id"));
  • The name of the uploaded file:

    Copy to clipboard
    \Cloudinary\Uploader::upload('sample.jpg', array("use_filename" => TRUE));
  • An image, a video, or a RAW file:

    Copy to clipboard
    \Cloudinary\Uploader::upload("spreadsheet.xls", array("resource_type" => "auto"));

For details on all the upload options, see the related Cloudinary documentation.

Conclusion

Uploading PHP images files to Cloudinary] is hassle free. Once you’ve completed that task, you no longer need to store files on your host server. Do take advantage of dedicated cloud-storage services like Cloudinary, which also performs the invaluable task of serving files securely to web apps through content delivery networks (CDNs).


Want to Learn More About File Uploads?

Recent Blog Posts

Our $2B Valuation

By
Blackstone Growth Invests in Cloudinary

When we started our journey in 2012, we were looking to improve our lives as developers by making it easier for us to handle the arduous tasks of handling images and videos in our code. That initial line of developer code has evolved into a full suite of media experience solutions driven by a mission that gradually revealed itself over the course of the past 10 years: help companies unleash the full potential of their media to create the most engaging visual experiences.

Read more
Direct-to-Consumer E-Commerce Requires Compelling Visual Experiences

When brands like you adopt a direct–to-consumer (DTC) e-commerce approach with no involvement of retailers or marketplaces, you gain direct and timely insight into evolving shopping behaviors. Accordingly, you can accommodate shoppers’ preferences by continually adjusting your product offering and interspersing the shopping journey with moments of excitement and intrigue. Opportunities abound for you to cultivate engaging customer relationships.

Read more
Automatically Translating Videos for an International Audience

No matter your business focus—public service, B2B integration, recruitment—multimedia, in particular video, is remarkably effective in communicating with the audience. Before, making video accessible to diverse viewers involved tasks galore, such as eliciting the service of production studios to manually dub, transcribe, and add subtitles. Those operations were costly and slow, especially for globally destined content.

Read more
Cloudinary Helps Minted Manage Its Image-Generation Pipeline at Scale

Shoppers return time and again to Minted’s global online community of independent artists and designers because they know they can count on unique, statement-making products of the highest quality there. Concurrently, the visual imagery on Minted.com must do justice to the designs into which the creators have poured their hearts and souls. For Minted’s VP of Engineering David Lien, “Because we are a premium brand, we need to ensure that every single one of our product images matches the selected configuration exactly. For example, if you pick an 18x24 art print on blue canvas, we will show that exact combination on the hero images in the PDF.”

Read more
Highlights on ImageCon 2021 and a Preview of ImageCon 2022

New year, same trend! Visual media will continue to play a monumental role in driving online conversions. To keep up with visual-experience trends and best practices, Cloudinary holds an annual conference called ImageCon, a one-of-a-kind event that helps attendees create the most engaging visual experiences possible.

Read more