PHP image and video upload

Last updated: Nov-14-2022

Cloudinary provides an API for uploading images, videos, and any other kind of file to Cloudinary. Files uploaded to Cloudinary are stored safely in the cloud with optional secure backups and revision history. Cloudinary's APIs allow secure uploading from your servers, directly from your visitors' browsers or mobile applications, or fetched via remote public URLs.

Cloudinary's PHP Library wraps Cloudinary's upload API and simplifies the integration. PHP methods are available for easily performing PHP image and video uploads to Cloudinary and PHP view helper methods are available for uploading directly from a browser to Cloudinary.

This page covers common usage patterns for PHP image and video upload with Cloudinary.

For details on all available upload options and parameters, see the Media upload documentation, and the upload method of the Upload API Reference.

Tip
Cloudinary's Upload widget provides an alternative to using a Cloudinary SDK to add upload functionality to your application, eliminating the need to develop in-house interactive upload capabilities. The upload widget is an interactive, feature rich, simple-to-integrate user interface that enables you to add Cloudinary upload support to your website. The widget can be easily embedded in your web application with just a few lines of JavaScript code. See the Upload widget documentation for detailed information.

Upload widget main screen

Server-side upload

You can upload images, videos, or any other raw file to Cloudinary from your PHP code. Uploading is done over HTTPS using a secure protocol based on your product envrionment's api_key and api_secret parameters.

PHP image upload

Use the UploadApi class to upload assets to Cloudinary.

Copy to clipboard
use Cloudinary\Api\Upload\UploadApi;

The following method of the UploadApi class uploads an image to Cloudinary:

Copy to clipboard
public function upload($file, $options = [])

For example, uploading a local image file named 'my_image.jpg':

Copy to clipboard
(new UploadApi())->upload('my_image.jpg')

The file to upload can be specified as a local path, a remote HTTP or HTTPS URL, a whitelisted storage bucket (S3 or Google Storage) URL, a base64 data URI, or an FTP URL. For details, see File source options.

For details on all available upload options and parameters, see the Media upload documentation, and the upload method of the Upload API Reference.

PHP video upload

You upload videos in exactly the same way as images. The SDK also supports automatically uploading large files to Cloudinary in chunks if the file to upload is larger than a defined chunk_size parameter, by default 20000000 Bytes (= 20 MB).

The following example uploads dog.mp4 to Cloudinary in chunks of 6 MB, and stores it in a bi-level folder structure with the public ID dog_closeup. It also performs two eager transformations that resize the video to a square and a small rectangle.

Copy to clipboard
(new UploadApi())->upload('dog.mp4', [
  'resource_type' => 'video',
  'public_id' => 'myfolder/mysubfolder/dog_closeup',
  'chunk_size' => 6000000,
  'eager' => [
    ['width' => 300, 'height' => 300, 'crop' => 'pad'], 
    ['width' => 160, 'height' => 100, 'crop' => 'crop', 'gravity' => 'south']], 
  'eager_async' => true, 
  'eager_notification_url' => 'https://mysite.example.com/notify_endpoint']
)

Upload response

By default, uploading is performed synchronously. Once finished, the uploaded image or video is immediately available for transformation and delivery. An upload call returns a Hash with content similar to the following:

Copy to clipboard
Array
(
  [public_id] => c87hg9xfxrd4itiim3t0
  [version] => 1571218607
  [signature] => f8645b000be7d717599affc89a068157e4748276
  [width] => 864
  [height] => 576
  [format] => jpg
  [resource_type] => image
  [created_at] => 2017-06-23T13:59:18Z
  [bytes] => 120253
  [type] => upload
  [url] => http://res.cloudinary.com/demo/image/upload/v1571218607/c87hg9xfxrd4itiim3t0.jpg
  [secure_url] => https://res.cloudinary.com/demo/image/upload/v1571218607/c87hg9xfxrd4itiim3t0.jpg
)

The response includes HTTP and HTTPS URLs for accessing the uploaded media asset as well as additional information regarding the uploaded asset: The Public ID, resource type, width and height, file format, file size in bytes, a signature for verifying the response and more.

Related topics

  • For more information on uploading media assets, see the Media upload documentation.
  • For details on all available upload parameters, see the upload method of the Upload API Reference.

Direct uploading from the browser

The upload sample mentioned above allows your server-side PHP code to upload media assets to Cloudinary. In this flow, if you have a web form that allows your users to upload images or videos, the media file's data is first sent to your server and only then uploaded to Cloudinary.

A more efficient and powerful option is to allow your users to upload images and videos in your client-side code directly from the browser to Cloudinary instead of going through your servers. This method allows for faster uploading and a better user experience. It also reduces load from your servers and reduces the complexity of your PHP applications.

You can upload directly from the browser using signed or unsigned calls to the upload endpoint, as shown in the Upload multiple files using a form examples.

For signed uploads from your client-side code, a secure signature must be generated in your server-side PHP code. You can use the api_sign_request method to generate SHA signatures:

Copy to clipboard
ApiUtils::signParameters($params_to_sign, $api_secret);

See also

  • For more information on uploading media assets, see the Media upload documentation.
  • For details on all available upload parameters, see the upload method of the Upload API Reference.
  • Get an overview of the PHP SDK, and the various configuration options.
  • See examples of powerful image and video transformations using PHP code
    and see our image transformations and video transformation docs.
  • Check out Cloudinary's asset administration capabilities, for example, renaming and deleting assets, adding tags and metadata to assets, and searching for assets.
  • See the PHP SDK Migration guide for more information on migrating to version 2 of the PHP SDK.

✔️ Feedback sent!

Rate this page: