限制仅上传到 mp3

Restricting upload to mp3 only

我一直在研究 PHP 允许人们将 MP3 文件上传到服务器的脚本:

$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$fileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $uploadOk = 1;
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 150000000000000000000000000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}

// Allow certain file formats
if ($fileType != "audio/mpeg" && $fileType != "audio/mpeg3" && $fileType != "audio/mp3"
&& $fileType != "audio/x-mpeg" && $fileType != "audio/x-mp3" && $fileType != 'audio/x-mpeg3' 
&& $fileType != 'audio/x-mpg' && $fileType != "audio/x-mpegaudio" && $fileType != "audio/mpg" 
&& $fileType != "audio/x-mpg") {
     echo "Invalid Audio Type, please use MP3 Format.";
    $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

在测试期间,我已经能够通过禁用限制来上传音频文件,所以我知道该程序可以正常工作,但我不断收到与下面的错误类似的错误。此代码 returns:

Notice: Undefined index: file in /uploadsong.php on line 6
Invalid Audio Type, please use MP3 Format.Sorry, your file was not uploaded.

我试过上传不同的 mp3 文件,但似乎都失败了。有人可以建议一种修复这些错误的方法吗?我已经在网上搜索了一个半多小时,但没有成功。我也知道安全是一个问题,我计划在未来进一步解决这个问题,但如果有人有任何提示,我将不胜感激。

改变

$fileType = pathinfo($target_file,PATHINFO_EXTENSION);

这是 returning ".mp3" 或类似文件

$fileType = $_FILES["fileToUpload"]["type"]

这将 return "audio/mpeg" 您正在检查的条件