1

I have one question for the video url.

I am trying to make a video upload. so this is my basic video upload code :

<?php

include_once '../includes.php';

if(isset($_POST['submit']))
{
    $name = $_FILES['file']['name'];
    $temp = $_FILES['file']['tmp_name'];

    move_uploaded_file($temp,"uploaded/".$name);
    $url = "http://localhost:8888/videouploadandplayback/uploaded/$name";
    mysql_query("INSERT INTO `video` VALUE ('','$name','$url')");
}

?>

in this code $url section is video url.

and this is my watch.php code:

<?php

if(isset($_GET['id']))
{
    $id = $_GET['id'];
    $query = mysql_query("SELECT * FROM `video` WHERE id='$id'");
    while($row = mysql_fetch_assoc($query))
    {
        $name = $row['name'];
        $url = $row['url'];
    }

    echo "You are watching ".$name."<br />";
    echo "<video id='my_video_1' class='video-js vjs-default-skin' 
      controls preload='none' width='640px' height='267px' data-setup='{}'>
    <source src='$url' type='video/mp4' />
  </video>";
}
else
{
    echo "Error!";
}

?>

in this code <source src='$url' type='video/mp4' /> section shows like this :

<source src="http://localhost:8888/videouploadandplayback/uploaded/158382524199979_46659.mp4" type="video/mp4">

but i want to change it for example like this :

<source src="http://localhost:8888/videouploadandplayback/uploaded/x5Wa88Bq" type="video/mp4">

How do I make a URL (Like Youtube)in this way?

2 Answers2

3

Just generate your database-entry using an auto-increment id to ensure unique values per file uploaded.

Then, you could simple convert the (decimal) id to a string with another base - and you have a youtoube-like-id:

$youtubeLikeId = base_convert($actual_id, 10, 35);

example values:

$youtubeLikeId = base_convert(21122544, 10, 35); //returns e2mv9

to get the original (surogate) id back, just reverse the base conversion:

$actual_id = base_convert("e2mv9", 35,10); //returns 21122544

Sidenode: Youtube uses an enhanced conversion by also using capital-letters.

dognose
  • 20,360
  • 9
  • 61
  • 107
-1

Alright, first of all, your code you're using there is vulnerable.($_GET['id'] part) You should check out SQL Injection and sanitizing your code. The most important thing is to stay safe.

I'd start with making a unique name for your file first (cool looking, youtube style if that's what you're looking for.)

You can solve this in any way you like, here's my very simplistic example.

//if no errors
$name = $_FILES['file']['name'];//Our name 
$temp = $_FILES['file']['tmp_name'];
$ext = pathinfo($name, PATHINFO_EXTENSION); //Get the extension before we make any changes.

$name =md5($name.microtime());//md5 of our name and microtime, this makes sure the encryption is random most of the time, because microtime is always changing.
$name = substr($name, 0,8); //Cutting off the part we don't need

move_uploaded_file($temp,"uploaded/".$name.'.'.$ext); //Storing out file

Now, lets add this to a database. -->

+----+----------+------+
| id |   name   | ext  |
+----+----------+------+
| x  | x5Wa88Bq | .mp4 |
+----+----------+------+

I made this database assuming you will always have a same path to your uploads, if not then you will have to go with a different solution.

 //?Selecting from database by id, if no errors     $name = $row['name'];

 echo "You are watching ".$name."<br />";
 echo "<video id='my_video_1' class='video-js vjs-default-skin' 
 controls preload='none' width='640px' height='267px' data-setup='{}'>
 <source src='load.php?v=$name' type='video/mp4' />   </video>";

Now we should create a load.php file, which will actually point somewhere to our video.

load.php ->

$v = $_GET['v']; //Sanitise first, don't use like this.

//Compare with database, if true get name and extension
$name = $row['name'];
$ext = $row['ext'];
$url = 'http://localhost:8888/videouploadandplayback/uploaded/';


if(!empty($v)){
    header("location:"$url.$name.$ext."");  
}

Our load.php now checks if there is a video with a specific name (ex. x5Wa88Bq), since that file exists it points it to the correct directory. The code that handles the video realises that load.php?v=$name points to a video (or not) and plays it (doesn't).

Now if you want to do more 'styling', you can check out .htaccess guides. It will help you out with rewriting your URL's. And hopefully then you could do

<source src='$name' type='video/mp4' /> 

Cheers.

Community
  • 1
  • 1
Sidetik
  • 610
  • 2
  • 9
  • 16
  • Thanks for your best reply. I try it but when i posted video video name looks like this => `a7a34c9a.mp4` in database table –  Jul 09 '14 at 19:36
  • Ah, the problem was I concatenated $name and $ext before actually adding it to the 'database'. You can check out the code again, i've edited it. (The first code block) – Sidetik Jul 09 '14 at 19:41
  • i am not using load page i am using video.php page this page shows all the video link. –  Jul 09 '14 at 19:45
  • Not really following you right now. I've changed the code so it won't add .mp4 into the database anymore. – Sidetik Jul 09 '14 at 19:56
  • Thanks for your reply dear. Can you tell me i am not using load.php page i have video.php page this page have all the video link. Click the link then open video page.how can i do this page. This is my last question from you :) Thank you so much –  Jul 09 '14 at 19:58
  • my video.php page code is this : `$name
    ";}?>`
    –  Jul 09 '14 at 20:01
  • Your href should be watch.php?id=$id – Sidetik Jul 09 '14 at 20:32
  • can you tell me watch.php is true. Because video not showing. –  Jul 09 '14 at 20:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57056/discussion-between-ecowebtr-and-sidetik). –  Jul 09 '14 at 20:49
  • 1
    Cutting of a md5-hash just to get some shorter "id" is bad. Even using a full-md5-hash as id is bad. Hashes are NO ids. An id needs to be unique. Hashes aren't. – dognose Jul 09 '14 at 23:04
  • While this is true, I wanted to give him the basic idea. Anyway, [check this out](http://stackoverflow.com/questions/2444321/how-are-hash-functions-like-md5-unique). – Sidetik Jul 10 '14 at 08:01