PHP DevCenter
oreilly.comSafari Books Online.Conferences.


Creating MyTube with Flex and PHP
Pages: 1, 2, 3, 4

When I navigate to the addmovie.html page, I see the screenshot shown in Figure 1.

Figure 1. The page for uploading movies
Figure 1. The page for uploading movies

From there, you can click Upload to send the movie to the server for processing.

The upload.php script is pretty rudimentary. To put it into production, you would need to add some error checking. The biggest problem with this script is in its ability to handle large movies. Large movies take a while to convert—way too long to have the user wait for the page to return.

To support large movies (more than 10 seconds of video or so), I recommend simply copying the movie into a batch directory, then informing the user that the movie will appear later on the site. Then, have another script that processes the movies in that directory.

It's worth stepping back here a moment to cover why I'm doing the conversion to Flash video. Sure, I need Flash video to view movies in the Flash Player. But were it not for that, were I to keep everything in its original format, I would need to have code that would show each movie with its own native player as well as allow users to find and install the player that's appropriate to their system. That would be a lot of work and a major pain. The advantage of normalizing all the video to Flash video—and to use a Flash Player written in Flex—is that it runs almost anywhere.

The next step is to build a simple HTML/Flash interface that's similar in form to YouTube.

Building the HTML/Flash Interface

Building a Flash movie that plays a selected movie provided to it through a URL starts with creating a new Flex project in Adobe Flex Builder™ 2. Then, you create a Flex application called simplemovie.mxml. The contents of this file are shown in Listing 4.

Listing 4. simplemovie.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="" layout="absolute">
<mx:VBox backgroundColor="white" width="400" height="335">
  <mx:VideoDisplay width="400" height="300" id="videoPlayer"
    source="{}" />
  <mx:HBox width="100%" horizontalAlign="center">
    <mx:Button label="Play" click="" />

This simple Flex application has two active elements: a VideoDisplay element that plays the video and a button labeled Play that users can click to restart the video after it's finished.

The VideoDisplay element takes a source attribute that contains the URL for the FLV video of the movie. In this case, it takes an application parameter provided using the FlashVars attribute on the <object> or <embed> tag within the HTML.

Using Flex Builder, you compile the simplemovie.mxml application into its corresponding simplemovie.swf file and move it from the bin directory into the PHP documents directory. From there, you build the PHP page that will host the movie. This page is shown in Listing 5.

Listing 5. simptest.php
require "DB.php";

$moviebase = 'http://localhost:8080/movies/';

$dsn = 'mysql://root@localhost/movies';
$db =& DB::connect( $dsn );
if ( PEAR::isError( $db ) ) { die($db->getMessage()); }

$source = null;
$movieId = 1;
if ( array_key_exists( 'movie', $_GET ) )
  $movieId = $_GET['movie'];

$movies = array();
$res = $db->query( 'SELECT movieId, source, title FROM movies' );
while( $row = $res->fetchrow( ) ) {
  $movies []= $row;
  if ( $row[0] == $movieId )
    $source = $row[1];

if ( $source == null )
    $source = $movies[0][1];
<tr><td valign="top">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="400" 
<param name="movie" value="simplemovie.swf" />
<param name="quality" value="high" />
<param name="flashVars" value="movie=<?php echo( $moviebase.$source ) ?>">
<embed src="simplemovie.swf" quality="high"
  width="400" height="335" play="true"
  flashVars="movie=<?php echo( $moviebase.$source ) ?>"
</td><td valign="top">
foreach( $movies as $movie ) {
<a href="simptest.php?movie=<?php echo( $movie[0] )?>"><?php echo( $movie[2] )?></a><br/>

The script starts by connecting to the database and getting the list of movies. While it's doing that, it also looks to see whether any of the movie IDs match the one passed into the URL. If it finds a match, it remembers that as the movie variable that will be passed to the simplemovie.swf file through the flashVars parameter.

The next section starts the HTML and creates the <object> and <embed> tags that host the simplemovie.swf player; it also provides it with the correct URL of the movie. After that, a small list of the available movies with links back to this page are shown. The links have the correct ID for each movie embedded in them.

Pages: 1, 2, 3, 4

Next Pagearrow

Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!

Sponsored by: