php 如何使用PHP上传音频文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20283016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:01:33  来源:igfitidea点击:

How to upload a audio file using PHP?

phphtml

提问by DKBHOI

I wanted to upload a audio file using html and php .It always returns the error message(Invalid file).please kindly help me. I am follow this url- https://forums.digitalpoint.com/threads/mp3-file-upload-in-php.1174500/;

我想使用 html 和 php 上传音频文件。它总是返回错误消息(无效文件)。请帮助我。我正在关注这个 url- https://forums.digitalpoint.com/threads/mp3-file-upload-in-php.1174500/;

http://p2p.wrox.com/php-how/53040-how-upload-mp3-file-using-php.html

http://p2p.wrox.com/php-how/53040-how-upload-mp3-file-using-php.html

This is the code I am using-

这是我正在使用的代码-

<form enctype="multipart/form-data" action="sound_action.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE"/>
    Choose a file to upload: <input name="file" type="file" /><br />
    <input type="submit" value="Upload File" />
</form>

<?php
    if ((($_FILES["file"]["type"] == "audio/mp3")
    || ($_FILES["file"]["type"] == "audio/mp4")
    || ($_FILES["file"]["type"] == "audio/wav"))
    && ($_FILES["file"]["size"] < 1000000))
    {
        if ($_FILES["file"]["error"] > 0)
        {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
        else
        {
            echo "Upload: " . $_FILES["file"]["name"] . "<br />";
            echo "Type: " . $_FILES["file"]["type"] . "<br />";
            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
            echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

            if (file_exists("upload/" . $_FILES["file"]["name"]))
            {
                echo $_FILES["file"]["name"] . " already exists. ";
            }
            else
            {
                move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
                echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
            }
        }
    }
    else
    {
        echo "Invalid file";
    }
?>

回答by Philipp

The mime type of mp3 is audio/mpegfor mp4 you have to use video/mp4and wave is audio/x-wavor audio/wav.

mp3 的 mime 类型audio/mpeg用于您必须使用的 mp4,video/mp4而 wave 是audio/x-wavaudio/wav

Also you should increase the file size, because this argument is given in bytes and 1000000 is less than 1mb. Maybe you have also to increase the upload file size in php.ini

您还应该增加文件大小,因为此参数以字节为单位给出,而 1000000 小于 1mb。也许您还必须增加 php.ini 中的上传文件大小

回答by jogesh_pi

There is an alternate way to validate a file, But it is a basic way to check the file validation not sure if file holds the correct type of not.

有一种验证文件的替代方法,但它是检查文件验证的基本方法,不确定文件是否包含正确的类型。

$valid_extension = array('.mp3', '.mp4', '.wav');
$file_extension = strtolower( strrchr( $_FILES["file"]["name"], "." ) );

if( in_array( $file_extension, $valid_extension ) && 
    $_FILES["file"]["size"] < 1000000 ){
    // Rest Logic Here
}
else
{
    print_r( $_FILES );
}

Note:

笔记:

这是一个基本的文件验证。

回答by De-Coder

You are missing an if clause :

您缺少一个 if 子句:

if(isset($_POST['name_submit_button']))
{
   //Whole Logic of program
}