How to use PHP to implement a simple online music download and playback system

PHPz
Release: 2023-09-25 13:18:02
Original
1337 people have browsed it

How to use PHP to implement a simple online music download and playback system

How to use PHP to implement a simple online music download and playback system

As network technology continues to develop today, music has become an indispensable part of people's lives. The demand for online music downloading and playing systems is also growing. This article will introduce how to use PHP to write a simple online music download and playback system, and provide specific code examples.

1. Build environment and database

First, we need a server environment to run PHP and create a database to store information about music files. Use XAMPP or other similar tools to set up a PHP running environment, and use MySQL to create a database named "music". Create a table named "songs" in the "music" database. The structure of the table is as follows:

CREATE TABLE `songs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `artist` varchar(255) NOT NULL,
  `file` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Copy after login

2. Upload music files

In the system, users can pass a simple form to upload music files and save the files to the specified directory on the server. Create a file named "upload.php" and add the following code:

<?php
if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $artist = $_POST['artist'];
    $file = $_FILES['file']['name'];
    $targetDir = "./uploads/";
    $targetFile = $targetDir . basename($file);
    
    // 保存文件
    if(move_uploaded_file($_FILES['file']['tmp_name'], $targetFile)){
        // 将文件信息保存到数据库
        $conn = new mysqli('localhost', 'root', '', 'music');
        $sql = "INSERT INTO songs (name, artist, file) VALUES ('$name', '$artist', '$file')";
        $conn->query($sql);
        $conn->close();
        echo "上传成功!";
    }else{
        echo "上传失败!";
    }
}
?>
<form method="POST" action="" enctype="multipart/form-data">
    歌曲名:<input type="text" name="name" required><br>
    歌手:<input type="text" name="artist" required><br>
    音乐文件:<input type="file" name="file" required><br>
    <input type="submit" name="submit" value="上传">
</form>
Copy after login

This code will receive the music files submitted by the user and save them to the "uploads" folder in the specified directory on the server. At the same time, the name, artist and file name of the music file are saved to the database.

3. Display the music list

In the system, users can view the uploaded music list. Create a file named "index.php" and add the following code:

<?php
$conn = new mysqli('localhost', 'root', '', 'music');
$sql = "SELECT * FROM songs";
$result = $conn->query($sql);
?>

<table>
    <tr>
        <th>歌曲名</th>
        <th>歌手</th>
        <th>操作</th>
    </tr>
    <?php while($row = $result->fetch_assoc()): ?>
    <tr>
        <td><?php echo $row['name']; ?></td>
        <td><?php echo $row['artist']; ?></td>
        <td>
            <a href="play.php?id=<?php echo $row['id']; ?>">播放</a>
            <a href="download.php?file=<?php echo $row['file']; ?>">下载</a>
        </td>
    </tr>
    <?php endwhile; ?>
</table>
Copy after login

In the above code, the "song" table in the database is queried and the name of the music file, the singer and the name of the music file are added using a loop statement. Action links are displayed in a table.

4. Play music

When the user clicks the "play" link in the music list, we need to use a file named "play.php" to play the music. Create this file and add the following code:

<?php
$id = $_GET['id'];
$conn = new mysqli('localhost', 'root', '', 'music');
$sql = "SELECT * FROM songs WHERE id=$id";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
$music = "./uploads/" . $row['file'];
?>

<audio controls autoplay>
    <source src="<?php echo $music; ?>" type="audio/mpeg">
    你的浏览器不支持音频播放。
</audio>
Copy after login

In the above code, by obtaining the URL parameter "id" of the "play.php" file, query the corresponding music file information in the database and change the path of the music file Displayed in an HTML5 audio player.

5. Download Music

When the user clicks the "Download" link in the music list, we need to use a file named "download.php" to implement the download function of the music file. Create the file and add the following code:

<?php
$file = $_GET['file'];
$filepath = './uploads/' . $file;
header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename="'.$file.'"');
header('Content-Length: ' . filesize($filepath));
readfile($filepath);
?>
Copy after login

In the above code, by obtaining the URL parameter "file" of the "download.php" file, the music file is downloaded to the user's computer as an attachment according to the file path. middle.

6. Summary

Through the above code examples, we can implement a simple online music download and playback system. Users can upload music files, and the system will save the files to the specified directory on the server and save relevant information to the database. Users can choose to play or download music files by viewing the music list. Although this system is simple, it can provide users with basic online music services. Of course, we can also expand and optimize according to needs.

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!