By storing media files to MySQL database make easier to retrieve files uploaded by the user or in the specific category.
For this require storing file to the server and save the reference to the database.
In the tutorial, I show how you can upload and store video to MySQL database table with PHP.
2. By storing media files to MySQL database make easier to retrieve files
uploaded by the user or in the specific category.
For this require storing file to the server and save reference to the database.
3. Contents -
1. Table structure
2. Configuration
3. Upload and Store Video
4. Read Videos
5. Conclusion
4. 1. Table structure
I am using videos table in the example.
CREATE TABLE `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;
7. 3. Upload and Store Video
Create a new directory videos in the project root.
HTML
Create a file element and a submit button in the <form>.
8. PHP
On <form> submit check file extension is valid or not and also check file size is
not greater than 5MB.
Upload the file to videos directory and insert file name and location in videos
table.
14. <?php
$fetchVideos = mysqli_query($con, "SELECT location FROM 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>
15. 4. Conclusion
Store video file in a directory and insert file location in a MySQL database table.
Display file using <video> element.
Before setting max file size for file size validation make sure to check
post_max_size and upload_max_filesize in php.ini file and update it accordingly
if required.
16. Share
For More PHP related tutorial
as well as other programming tutorials visit :
Makitweb.com