contact
WA: 0822-8846-7823
SMS: 0822-8846-7823
Call: 0822-8846-7823
BBM: D7DB3491
ebsofmail@gmail.com

Upload dan View Video dengan PHP MYSQLi

Posted in PHP MYSQLi Life at 05 Juni 2020 With 0 Comments

Selamat malam sahabat suckittrees.com .. lama tidak upload artikel.. kali ini kita akan Membahas Cara Upload Video dengan PHP MYSQLi, ok.. langsung saja.. 

Buat tabel di database dengan nama "db_suckittrees" , kemudian buat tabel dengan nama "tbl_videos"

CREATE TABLE `tbl_videos` (
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`location` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

Kemudian kita buat file config.php

<?php

$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "db_suckittrees"; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
?>

Membuat File Upload dengan HTML nya

<!doctype html>
<html>
<head>
<?php
include("config.php");

if(isset($_POST['but_upload'])){
$maxsize = 5242880; // 5MB

$name = $_FILES['file']['name'];
$target_dir = "videos/";
$target_file = $target_dir . $_FILES["file"]["name"];

// Select file type
$videoFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Valid file extensions
$extensions_arr = array("mp4","avi","3gp","mov","mpeg");

// Check extension
if( in_array($videoFileType,$extensions_arr) ){

// Check file size
if(($_FILES['file']['size'] >= $maxsize) || ($_FILES["file"]["size"] == 0)) {
echo "File too large. File must be less than 5MB.";
}else{
// Upload
if(move_uploaded_file($_FILES['file']['tmp_name'],$target_file)){
// Insert record
$query = "INSERT INTO tbl_videos(name,location) VALUES('".$name."','".$target_file."')";

mysqli_query($con,$query);
echo "Upload successfully.";
}
}

}else{
echo "Invalid file extension.";
}

}
?>
</head>
<body>
<form method="post" action="" enctype='multipart/form-data'>
<input type='file' name='file' />
<input type='submit' value='Upload' name='but_upload'>
</form>

</body>
</html>

 

Untuk menampilkan video dengan PHP MYSQLi , berikut codenya : 

<?php
include("config.php");
?>
<!doctype html>
<html>
<head>
<style>
video{
float: left;
}
</style>
</head>
<body>
<div>

<?php
$fetchVideos = mysqli_query($con, "SELECT location FROM tbl_videos ORDER BY id DESC");
while($row = mysqli_fetch_assoc($fetchVideos)){
$location = $row['location'];

echo "<div >";
echo "<video src='".$location."' controls width='320px' height='200px' >";
echo "</div>";
}
?>

</div>

</body>
</html>

Upload Video PHP MYSQLi

Artikel Rekomendasi

Artikel Terkait

Diskusi