PHP: How to upload and display video in php - InfoHifi

How to upload and display videos in php - mysql




Source Code of how to upload and display video in php mysql

index.php file

<!DOCTYPE html >
<html>
<head>
<title>video upload</title>
</head>
<body>

    <h1><a href="video.php">VIDEOS</a></h1>

<form  action="index.php" method="post"  
enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="UPLOAD" name="upload" />

</form>
</body>
</html>

<?php

include ('db.php');

if (isset($_POST['upload'])){

$name = $_FILES['file']['name'];

$tmp = $_FILES['file']['tmp_name'];


move_uploaded_file($tmp,"videos/" . $name);

$sql = "INSERT INTO videos (nameVALUES ('$name')";

$res =  mysqli_query($con$sql);

if ($res == 1){

    echo "<h1> video inserted successfully </h1>";
}
}
?>

db.php file


<?php

$con = mysqli_connect("localhost","root","","myvideo");

?>

video.php file


<?php

include ('db.php');

$sql = "select * from videos";

$res = mysqli_query($con,$sql);

echo "<h1>MYVIDEOS</h1>";
while ($row = mysqli_fetch_assoc($res)) {
    
    $id = $row['v_id'];
    $name = $row['name'];

    echo  "<h4><a href='watch.php?id=$id' > ". $name . 
"</a></h4>";
}
?>

watch.php file


<?php

include ('db.php');

if (isset($_GET['id'])){
    $id = $_GET['id'];
$sql = "select name from videos where v_id='$id'";

$res = mysqli_query($con,$sql);

$row = mysqli_fetch_assoc($res);
$name = $row['name'];

echo "<h1>you are watching:".$name." </h1><br/>";

?>

    <video width="600" height="316" controls>
        
        <source src="videos/<?php  echo $name?>" 
                type="video/mp4">

    </video>

<?php
}

?>


Post a Comment

0 Comments