Home >Backend Development >PHP Tutorial >How to use PHP to implement a simple online video upload and playback system

How to use PHP to implement a simple online video upload and playback system

WBOY
WBOYOriginal
2023-09-25 10:55:412071browse

How to use PHP to implement a simple online video upload and playback system

How to use PHP to implement a simple online video upload and playback system

With the development of the Internet, video content has gradually become one of the important ways for people to obtain information and entertainment. one. In order to meet users' needs for video upload and playback, we can use the PHP programming language to implement a simple online video upload and playback system.

The following will introduce how to use PHP to implement this system, including database creation, file upload and video playback functions.

  1. Create database

First, we need to create a MySQL database to store the video information uploaded by users. You can use the following SQL statement to create a database named "videos" and create a table named "video_info" in it to store video information:

CREATE DATABASE IF NOT EXISTS videos;
USE videos;
CREATE TABLE IF NOT EXISTS video_info (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    description TEXT NOT NULL,
    file_name VARCHAR(255) NOT NULL,
    uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
  1. File Upload

In PHP, you can use the $_FILES super global variable to obtain uploaded file information. We can create a page to receive video files uploaded by users and store them in a specified directory.

<?php
if(isset($_POST['upload'])){
    $title = $_POST['title'];
    $description = $_POST['description'];
    $file = $_FILES['video'];

    $file_name = $file['name'];
    $file_tmp = $file['tmp_name'];
    $file_size = $file['size'];
    $file_error = $file['error'];

    if($file_error === UPLOAD_ERR_OK){
        $upload_dir = 'uploads/';
        move_uploaded_file($file_tmp, $upload_dir . $file_name);

        // 将视频信息插入数据库
        $conn = new mysqli('localhost', 'root', 'password', 'videos');
        $conn->query("INSERT INTO video_info(title, description, file_name) VALUES ('$title', '$description', '$file_name')");

        echo "视频上传成功!";
    }else{
        echo "视频上传失败!";
    }
}
?>

<form action="" method="POST" enctype="multipart/form-data">
    <input type="text" name="title" placeholder="请输入视频标题" required><br>
    <textarea name="description" placeholder="请输入视频描述" required></textarea><br>
    <input type="file" name="video" required><br>
    <input type="submit" name="upload" value="上传视频">
</form>

In the above code, we first obtain the video title and description filled in by the user through the $_POST variable. Then, use the $_FILES['video'] variable to get the uploaded file information, including file name, temporary path, size and error code. If no errors occur during the upload process, the file is moved to the specified directory and the video information is inserted into the database.

  1. Video playback

In PHP, we can use the HTML5 video tag to implement the video playback function. We can create a page to get the video id, read the video information from the database based on the id, and play it through the video tag.

<?php
if(isset($_GET['id'])){
    $id = $_GET['id'];

    $conn = new mysqli('localhost', 'root', 'password', 'videos');
    $result = $conn->query("SELECT * FROM video_info WHERE id = $id");
    $video = $result->fetch_assoc();

    $file_name = $video['file_name'];
    $title = $video['title'];
    $description = $video['description'];
}
?>

<h1><?php echo $title; ?></h1>
<p><?php echo $description; ?></p>

<video controls>
    <source src="uploads/<?php echo $file_name; ?>" type="video/mp4">
    您的浏览器不支持HTML5视频标签
</video>

In the above code, we first obtain the id of the video through the $_GET variable, and then use the id to read the video information from the database. Next, we use the video tag to display the video title and description, and set the video source to the video file in the specified directory.

Through the above steps, we have completed the implementation of a simple online video upload and playback system. When a user uploads a video, the video will be stored in the specified directory and the video information will be inserted into the database. Users can access the corresponding playback page through the video ID, thereby realizing the online video playback function.

This is just a simple example. The actual online video upload and playback system may involve more functions and security considerations. But through the above code, you can understand how to use PHP to implement a basic online video upload and playback system.

The above is the detailed content of How to use PHP to implement a simple online video upload and playback system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn