Home  >  Article  >  Backend Development  >  How to develop a simple music player using PHP

How to develop a simple music player using PHP

WBOY
WBOYOriginal
2023-09-20 11:19:411455browse

How to develop a simple music player using PHP

How to use PHP to develop a simple music player

With the development of network technology, music players have become an indispensable part of people's daily lives. If you have basic PHP programming knowledge, then using PHP to develop a simple music player is a good choice. This article will introduce how to use PHP to implement a simple music player and provide specific code examples.

1. Create a database

First, we need to create a database to store music information. You can use MySQL or other relational database management systems to create a database named "music" and create a table named "songs" in it. The table structure is as follows:

CREATE TABLE songs (
id INT(11) AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
artist VARCHAR(255) NOT NULL,
file_path VARCHAR(255) NOT NULL
);

In this table, we need to store the title, artist and music file path (file_path) information.

2. Upload music files

Next, we need to provide the upload function for the music player so that users can upload music files to the server. In the form for uploading music files, the user needs to provide the title of the music, the artist, and select the music file to upload. When the user clicks the submit button, we can use PHP to process the form and save the music file to a specified location on the server. Here is an example PHP code:

if (isset($_POST['submit'])) {

$title = $_POST['title'];
$artist = $_POST['artist'];
$file_name = $_FILES['file']['name'];
$file_path = 'music/' . $file_name;

move_uploaded_file($_FILES['file']['tmp_name'], $file_path);

// 将音乐信息保存到数据库中
$query = "INSERT INTO songs (title, artist, file_path) 
          VALUES ('$title', '$artist', '$file_path')";
// 执行数据库插入操作
// ...

}

In the above code, we use $ _FILES variable to get the music files uploaded by the user, and use the move_uploaded_file() function to save the file to the specified location. Then, we insert the music information into the database.

3. Display the music list

Next, we need to display the uploaded music list for users to choose. You can use the following PHP code to query the music information in the database and display it to the user in the form of a list:

// Query the music information in the database
$query = "SELECT * FROM songs ";
// Execute database query operation
// ...

// Traverse the query results and generate a music list
while ($row = mysqli_fetch_assoc($result)) {

echo "<li><a href='player.php?song_id={$row['id']}'></a>{$row['title']} - {$row['artist']}</li>";

}

In the above code, we use the mysqli_fetch_assoc() function to traverse the query results and generate a music list. Each piece of music is a link, and the link address is "player.php?song_id=Music ID", so that when the user clicks on a piece of music, he can jump to the play page and play the selected music.

4. Play music

Finally, we need to create a play page (player.php) to play the selected music. You can use the following HTML and PHP code to create this page:


<?php
  if (isset($_GET['song_id'])) {
      $song_id = $_GET['song_id'];
      // 查询数据库中该音乐的文件路径
      $query = "SELECT file_path FROM songs WHERE id = $song_id";
      // 执行数据库查询操作
      // ...
      $row = mysqli_fetch_assoc($result);
      
      // 输出音乐播放器
      echo "<audio controls>
              <source src='{$row['file_path']}' type='audio/mpeg'>
            </audio>";
  }
?>


In the above code, we query the file path of the music from the database based on the music ID selected by the user, and use the HTML

Summary

Through the above steps, we successfully developed a simple music player using PHP. Users can upload music files and select music to play through the music list. When the user clicks on the music, it will jump to the play page and start playing the music. Of course, this is just a simple example and you can extend and optimize it according to your needs. I hope this article will be helpful for you to learn and understand the development of PHP music player!

The above is the detailed content of How to develop a simple music player using PHP. 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