How to use PHP to implement a simple music upload and playback system

王林
Release: 2023-09-24 09:52:01
Original
1439 people have browsed it

How to use PHP to implement a simple music upload and playback system

How to use PHP to implement a simple music upload and playback system

With the popularity of music and the development of the Internet, music upload and playback systems are becoming more and more popular. If you also want to implement a simple music upload and playback system in your website or application, then PHP is a very suitable choice. In this article, I will introduce you to how to use PHP to implement a simple music upload and playback system, including specific code examples.

First, we need to prepare a directory for storing music files. You can create a directory called "uploads" on your server and make sure it has write permissions. Next, let's create an HTML form for uploading music.

<!DOCTYPE html>
<html>
<head>
    <title>音乐上传</title>
</head>
<body>
    <h1>音乐上传</h1>
    <form action="upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="music">
        <input type="submit" value="上传">
    </form>
</body>
</html>
Copy after login

In the above HTML code, we have created a form through which the user can select the music file to upload. The form submission address is "upload.php". Next, let's implement the PHP code for the upload function.

<?php
$targetDir = "uploads/";
$fileName = $_FILES["music"]["name"];
$targetFile = $targetDir . basename($fileName);
$uploadSuccess = true;

if (move_uploaded_file($_FILES["music"]["tmp_name"], $targetFile)) {
    echo "音乐上传成功!";
} else {
    echo "音乐上传失败!";
}
?>
Copy after login

In the above PHP code, we first specified the saving directory of uploaded files as "uploads/". Then, we get the file name uploaded by the user through $_FILES"music" and splice it into the target file path. Next, use the move_uploaded_file() function to move the uploaded temporary file to the target file path. If the move is successful, the user will be prompted to upload successfully; otherwise, the user will be prompted to upload failed.

Now, we have implemented the music upload function. Next, let's implement the music playback function. First, let's create an HTML page that displays a list of uploaded music.

<!DOCTYPE html>
<html>
<head>
    <title>音乐播放</title>
</head>
<body>
    <h1>音乐播放</h1>
    <ul>
        <?php
        $musicDir = "uploads/";
        $musicFiles = glob($musicDir . "*");

        foreach ($musicFiles as $musicFile) {
            echo "<li><a href="$musicFile" target="_blank">$musicFile</a></li>";
        }
        ?>
    </ul>
</body>
</html>
Copy after login

In the above HTML code, we use the glob() function to get all the uploaded music files and display them in an unordered list through a loop.

Then, when the user clicks on the music file link, the music player can be opened in a new window. Here we use the target="_blank" attribute to achieve this.

The above is all the code required to implement a simple music upload and playback system using PHP. You can customize and extend it to suit your needs. Hope this article is helpful to you!

The above is the detailed content of How to use PHP to implement a simple music upload 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