Home arrow PHP arrow Page 4 - Video Streaming PHP Script Tutorial

The PHP script: Streamvideo.php - PHP

One of the most important features in a website is the ability to show videos to your visitors. There are a lot of video hosting solutions nowadays, with YouTube as the most popular. However, due to its popularity, there are lots of services -- even free services -- that let viewers download your video files after you've uploaded them to YouTube. If you need to restrict downloading of your files, keep reading.

TABLE OF CONTENTS:
  1. Video Streaming PHP Script Tutorial
  2. Design of the Application
  3. Upload video files and create a MySQL table
  4. The PHP script: Streamvideo.php
By: Codex-M
Rating: starstarstarstarstar / 34
January 27, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The video PHP is very similar to the PHP MP3 streaming script.

The things that are changed are the following:

1. Database table field names (though they are still very similar).

2. Minor parts of PHP validation.

3. The content type header, from the mp3 format to: header('Content-type: video/mpeg');


The third item is the most important change. If you are using this script for other video file types, use the appropriate content type header.

The revised PHP script is shown below:


<?php


//Validate text input

if (! preg_match('/^[-a-z.-@,'s]*$/i',$_GET['ID']))

{

die('Invalid name proved, the name may only contain a-z, A-Z, 0-9, "-", "_" and spaces.');

}

else

$empty=strlen($_GET['ID']);

if ($empty==0)

{

die('The text field cannot be empty');

}

else

{

//the input data is clean, retrieve text data input

$ID = $_GET['ID'];

}

//Connect to MySQL database after sanitizing the data

$username = "**********";

$password = "**********";

$hostname = "**********";

$database = "**********";

$dbhandle = mysql_connect($hostname, $username, $password)

or die("Unable to connect to MySQL");

//select a database to work with

$selected = mysql_select_db($database,$dbhandle)

or die("Could not select $database");

//Escape variables for use in MySQL

$ID = mysql_real_escape_string(stripslashes($ID));

// sending query

$result = mysql_query("SELECT `realurl` FROM `videostreaming` WHERE `id`='$ID'")

or die(mysql_error());

// store the record of the "example" table into $row

$row = mysql_fetch_array( $result )

or die("Invalid query: " . mysql_error());

// Print out the contents of the entry

$direction = $row['realurl'];

//close the connection

$path='http://'.$direction;

header('Content-type: video/mpeg');

header('Content-Length: '.filesize($path)); // provide file size

header("Expires: -1");

header("Cache-Control: no-store, no-cache, must-revalidate");

header("Cache-Control: post-check=0, pre-check=0", false);

readfile($path);

mysql_close($dbhandle);

?>


You can download the script and view the demo.



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: