Cloudinary Blog

Uploading PHP Files and Rich Media the Easy Way

Uploading PHP Files and Rich Media the Easy Way

Images and videos, called rich media as a whole, abound on the Internet. Many modern developer apps require that you manipulate and upload files to the server—a mundane, time-consuming, and error-prone chore. A saving grace is Cloudinary’s PHP SDK, with which you can easily and speedily upload multiple images and videos in PHP files.

Cloudinary is a media-management service through which you can file upload, transform, and manage rich media. For uploads from your app, whether desktop or mobile, Cloudinary offers an API, which you can integrate with your app. This post steps you through the process of uploading PHP files.


How to Optimize for Page Load Speed


File Uploads With Cloudinary

With Cloudinary’s upload API, you can transmit any kind of file for safe storage in the cloud, complete with secure backups and revision history. To use that API, all you need is Cloudinary’s open-source PHP library, which contains simple, 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, follow the steps below.

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.

2. Fetch 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
// 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.

3. Upload 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

Automation Frees Up PetRescue’s Staff to Help Pets Find Their Forever Homes

As we spend more time at home, many of us are adopting pets for the joy, companionship and a surprising range of health benefits. In Australia, where our nonprofit customer PetRescue is located, there’s a shortage of pets to adopt. Last August, the Guardian reported that dog shelters in Australia emptied and adoption fees for puppies were running as high as $AUS1800.

Read more
Cloudinary and Contentful Make Modern Content Management Easier

I am pleased to share that Cloudinary and Contentful have joined forces to further streamline the creation, processing, and delivery of online content through Cloudinary’s digital asset management (DAM) solution and advanced transformation and delivery capabilities for images and video. What’s more, the partnership delivers a headless approach to DAM. By leveraging APIs for media management tasks, marketers and developers alike benefit from an integrated stack of optimized assets for optimization and automation. As a result, page loads are fast and beautiful, and at scale—with less overhead and effort.

Read more
Introducing Cloudinary's Nuxt Module

Since its initial release in October 2016 by the Chopin brothers as a server-side framework that runs on top of Vue.js, Nuxt (aka Nuxt.js) has gained prominence in both intuitiveness and performance. The framework offers numerous built-in features based on a modular architecture, bringing ease and simplicity to web development. Not surprisingly, Nuxt.js has seen remarkable growth in adoption by the developer community along with accolades galore. At this writing, Nuxt has earned over 30K stars on GitHub and 96 active modules with over a million downloads per month. And the upward trend is ongoing.

Read more
How Quality and Quantity can go Hand in Hand

When it comes to quality versus quantity, you’ll often hear people say, “It’s the quality that counts, not the quantity”. While that’s true in many situations, there are also cases where you want both quality and quantity. You may have thousands of images on your website and you want them all to look great. This is especially important if your website allows users to upload their own content, for example, to sell their own products or services. You don't want their poor quality images to reflect badly on your brand.

Read more
Product Videos 101: What Makes Them Great?

A product’s benefits and usage, including its value proposition, features, and instructive details, are best demonstrated through video. Product-video types vary, depending on the funnel, channel, and audience, the most popular ones being demos, reviews, installation, and how-tos.

Read more