Media Write API: PHP Example - Upload Video Sample Code

ProductEdition
Video CloudProfessional, Enterprise

This is sample code for the PHP upload example, an application that uses a PHP page to upload a video to your media library. The PHP script uses the PHP Media API Wrapper, which you can obtain and find instructions for using on the Brightcove Open Source Project site.

The script handles the upload form and uploads the video. You will need to provide valid tokens for your account, replacing [[READ_TOKEN]] and [[WRITE_TOKEN]] with your own tokens.

Code for the form:

<html>
  <head>
    <title>File Upload Form</title>
  </head>
  <body>
    <h2>This form allows you to upload a Video to the Brightcove Video Cloud.</h2>
    <form action="BCLuploadVideo.php" method="post" enctype="multipart/form-data"><br>
      <p>Video Name: <input type="text" name="bcVideoName" size="50" /></p>
      <p>Video Description:<br/><textarea name="bcShortDescription" rows="5" cols="50"></textarea></p>
      <p>Type (or select) Filename: <input type="file" name="videoFile"></p>
      <p><input type="submit" value="Upload File"></p>
    </form>
  </body>
</html>

Code for the PHP script:

<?php

  // This code example uses the PHP Media API wrapper
  // For the PHP Media API wrapper, visit http://docs.brightcove.com/en/video-cloud/open-source/index.html

  // Include the BCMAPI Wrapper
  require('bc-mapi.php');
        
  // Instantiate the class, passing it our Brightcove API tokens (read, then write)
  $bc = new BCMAPI(
    '[[READ_TOKEN]]',
    '[[WRITE_TOKEN]]'
  );

  // Create an array of meta data from our form fields
  $metaData = array(
    'name' => $_POST['bcVideoName'],
    'shortDescription' => $_POST['bcShortDescription']
  );
  
  // Move the file out of 'tmp', or rename
  rename($_FILES['videoFile']['tmp_name'], '/tmp/' . $_FILES['videoFile']['name']);
  $file = '/tmp/' . $_FILES['videoFile']['name'];
  
  // Create a try/catch
  try {
    // Upload the video and save the video ID
    $id = $bc->createMedia('video', $file, $metaData);
          echo 'New video id: ';
          echo $id;
  } catch(Exception $error) {
    // Handle our error
    echo $error;
    die();
  }
?>

 

Post new comment

The content of this field is kept private and will not be shown publicly.
0

Comments