video upload,Download and display in php mysql - Web Development and Web Design Codes

Latest

Friday, May 26, 2017

video upload,Download and display in php mysql

PHP MySQL Video Upload Save to Database and Download

video upload ,display and download in php mysql
video upload ,display and download in php mysql

In This Lesson we will Learn how to Upload and Save Video file Into Database using PHP and MySQL.
You can Upload any Videos Easily To Desired Folder ,Save Information to Database and Download forcefully using php.
in the next lesson we will learn how to generate video thumbnail using php and FFMpeg..
How to Upload large file in php
Also Learn
1. PHP MySQL Pagination
2. PHP MySQL Login with SESSION
3. php mysql comment system
4. sort MySQL table column using PHP

5. PHP MySQL Shopping Cart

##What You will Need:

##Create create 4 files and 1 folder named 'videos' in same folder.
1.connect.php
2.index.php
3.upload.php
4. download.php
4.style.css
5. videos [folder name]
[update: create a new file download.php]
video upload and display in php mysql
video upload and display in php mysql

Database:
 Database Name: test
 Table name: videos
Using The Below SQL Commands for Creating Table 'videos'
 CREATE TABLE IF NOT EXISTS `videos` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `size` varchar(100) NOT NULL,
  `type` varchar(10) NOT NULL,
   PRIMARY KEY(id)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
 

Now copy The Below Codes for Certain File.
connect.php:
<?php
// Creating connection test is our database name
$conn = new mysqli('localhost', 'root', '','test');
// Checking connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>
 
index.php:
Create Index.php to Display Uploaded Videos with Download Link.
<?php
include("connect.php");
?>
<link rel="stylesheet" href="style.css" type="text/css"/>
<div id="content">
 <?php
 include('upload.php');
 $result=mysqli_query($conn,"SELECT*FROM videos");
 while($row=$result->fetch_array()){?>
<div id="video_player_box"> 
  <video id="video" width="300" height="200" controls>
  <source src="<?php echo 'videos/'.$row['name'];?>" type="video/<?php echo $row['type'];?>">
    Your browser does not support the video tag.
</video>
<div id="download">
    <button id="downloadbtn"><a href="download.php?file=<?php echo $row['name'];?>">Download Now</a></button>
  </div>
</div>

  <?php }?>
  </div>
upload.php:
Create Upload.php file to Display Upload form and Processing Upload Selected File.
Add the below HTML codes top of the upload.php file to Display Upload form.
<h2>Upload,Save and Download video Using PHP MySQL</h2>
<form method="POST" action="" enctype="multipart/form-data">
<input type="file" name="video"/>
<input type="submit" name="submit" value="Upload"/>
</form>
<?php
include("connect.php");
$errors=1;
 //Targeting Folder
 $target="videos/";
if(isset($_POST['submit'])){
 //Targeting Folder
 
 $target=$target.basename($_FILES['video']['name']);
 //Getting Selected video Type
 $type=pathinfo($target,PATHINFO_EXTENSION);
 //Allow Certain File Format To Upload
 if($type!='mp4' && $type!='3gp' && $type!='avi'){
  echo "Only mp4,3gp,avi file format are allowed to Upload";
   $errors=0;
 }
  //checking for Exsisting video Files
  if(file_exists($target)){
   echo "File Exist";
   $errors=0;
  }
   //Checking for File Size 1000000 bytes== 1MB ..here file limit is 2MB..You can Change the limit..
   //250*2000000==500MB
  $filesize=$_FILES['video']['size'];
   if($filesize>250*2000000){
    echo 'You Can not Upload Large File(more than 2MB) by Default ini setting..<a href="http://www.codenair.com/2018/03/how-to-upload-large-file-in-php.html">How to upload large file in php</a>'; 
       $errors=0;
   }
   if($errors == 0){
    echo ' Your File Not Uploaded';
   }else{
   //Moving The video file to Desired Directory
  $uplaod_success=move_uploaded_file($_FILES['video']['tmp_name'],$target);
  if($uplaod_success){
   //Getting Selected video Information
      $name=$_FILES['video']['name'];
      $size=$_FILES['video']['size'];
   $result=mysqli_query($conn,"INSERT INTO videos (name,size,type)VALUES('$name','$size','$type')");
   if($result==TRUE){
    echo "Your video '$name' Successfully Upload and Information Saved Our Database";
   }
  }
  }
 }
?>
download.php
<?php
if(isset($_GET['file']))
{
    $var_1 = $_GET['file'];
$dir = "videos/"; // trailing slash is important
$file = $dir . $var_1;
 
if (file_exists($file))
    {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
    }
}
?>

style.css:
Now add some CSS codes to Style our Index Page.
input[type=file]{
 padding:5px 0px 5px 5px;
 background:black;
 border-radius:5px;
}
input[type=submit]{
 background:black;
 color:white;
 padding:6px 8px;
 font-weight:bold;
 cursor:pointer;
 border-radius:5px;
}
form{
 padding-bottom:5px;
 border-bottom:2px solid green;
}
#content{
 width:800px;
 background:#99ccff;
 margin:auto;
 overflow:hidden;
}
#video_player_box{width:300px;border-right:2px solid red;border-bottom:2px solid red;float:left;background:#000; margin:0px auto;}
#download{ width:250px;background: #000; padding:6px; color:#CCC;}
#downloadbtn{
 background:#3973ac;
    padding:5px 7px;
    font-weight:bold;
    cursor:pointer;
    border-radius:5px;
}
#downloadbtn a{
 color:white;
 text-decoration:none;
}
 

Ok Now You have Successfully Created PHP MySQL Video upload and Download Script.Please Visit Again for More Codes.
Tag: upload video php,php file upload,upload file php,html file upload,file upload html,file upload script

No comments:

Post a Comment

Thank You for Your Comment