.mp3 文件类型上传

2022-01-09 00:00:00 mime mp3 upload php

我正在开发一个 PHP 上传脚本,它允许上传 .mp3 文件等.我创建了一个数组,它指定了允许的文件类型,包括 mp3,并将最大上传限制设置为 500MB:

I'm working on a PHP upload script which allows .mp3 file uploads amongst others. I've created an array which specifies permitted filetypes, including mp3s, and set a maximum upload limit of 500MB:

// define a constant for the maximum upload size
define ('MAX_FILE_SIZE', 5120000);

// create an array of permitted MIME types
$permitted = array('application/msword', 'application/pdf', 'text/plain', 'text/rtf', 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/tiff', 'application/zip', 'audio/mpeg', 'audio/mpeg3', 'audio/x-mpeg-3', 'video/mpeg', 'video/mp4', 'video/quicktime', 'video/x-ms-wmv', 'application/x-rar-compressed');

到目前为止,在测试中,所有指定的文件类型都已成功上传,但由于某种原因,它出现了 .mp3 错误.正如您在上面看到的,我已经包含了音频/mpeg、音频/mpeg3 和音频/x-mpeg-3,但它们似乎都没有什么不同.

So far in testing all specified filetypes have been successfully uploaded but for some reason it comes up with an error for .mp3. As you can see above I've included audio/mpeg, audio/mpeg3, and audio/x-mpeg-3 but none of them seem to make a difference.

有人可以提出问题所在并指出允许上传 .mp3 所需的音频类型吗?

Can someone suggest what the problem could be and also indicate which audio type is the one needed to allow .mp3 uploads?

谢谢

更新:我用来检查文件的代码如下:

Update: The code I'm using to run the check on the file is as follows:

// check that file is within the permitted size
        if ($_FILES['file-upload']['size'][$number] > 0 || $_FILES['file-upload']['size'][$number] <= MAX_FILE_SIZE) {
            $sizeOK = true;
        }

        // check that file is of an permitted MIME type
        foreach ($permitted as $type) {
            if ($type == $_FILES['file-upload']['type'][$number]) {
                $typeOK = true;
                break;
            }
        }

        if ($sizeOK && $typeOK) {
            switch($_FILES['file-upload']['error'][$number]) {
                case 0:
                    // check if a file of the same name has been uploaded
                    if (!file_exists(UPLOAD_DIR.$file)) {
                        // move the file to the upload folder and rename it
                        $success = move_uploaded_file($_FILES['file-upload']['tmp_name'][$number], UPLOAD_DIR.$file);
                    }
                    else {
                        // strip the extension off the upload filename
                        $filetypes = array('/.doc$/', '/.pdf$/', '/.txt$/', '/.rtf$/', '/.gif$/', '/.jpg$/', '/.jpeg$/', '/.png$/', '/.tiff$/', '/.mpeg$/', '/.mpg$/', '/.mp4$/', '/.mov$/', '/.wmv$/', '/.zip$/', '/.rar$/', '/.mp3$/');
                        $name = preg_replace($filetypes, '', $file);
                        // get the position of the final period in the filename
                        $period = strrpos($file, '.');
                        // use substr() to get the filename extension
                        // it starts one character after the period
                        $filenameExtension = substr($file, $period+1);
                        // get the next filename    
                        $newName = getNextFilename(UPLOAD_DIR, $name, $filenameExtension); 
                        $success = move_uploaded_file($_FILES['file-upload']['tmp_name'][$number], UPLOAD_DIR.$newName);
                    }
                    if ($success) {
                        $result[] = "$file uploaded successfully";
                    }
                    else {
                        $result[] = "Error uploading $file. Please try again.";
                    }
                    break;
                case 3:
                    $result[] = "Error uploading $file. Please try again.";
                default:
                    $result[] = "System error uploading $file. Contact webmaster.";
            }
        }
        elseif ($_FILES['file-upload']['error'][$number] == 4) {
            $result[] = 'No file selected';
        }
        else {
            $result[] = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: doc, pdf, txt, rtf, gif, jpg, png, tiff, mpeg, mpg, mp3, mp4, mov, wmv, zip, rar.";
        }

我得到底部 else 结果告诉我文件大小错误或扩展名不被允许.

I'm getting the bottom else result telling me either the file size is wrong or the extension isn't allowed.

更新 2:我已经运行了 _FILES 数组的 print_r,希望能提供更多信息.结果是:

Update 2: I've run a print_r of the _FILES array to hopefully provide a little more info. The results are:

数组([文件上传] => 数组([名称] => 数组([0] => 莫扎特.mp3[1] =>[2] =>)

Array ( [file-upload] => Array ( [name] => Array ( [0] => Mozart.mp3 [1] => [2] => )

        [type] => Array
            (
                [0] => audio/mpg
                [1] => 
                [2] => 
            )

        [tmp_name] => Array
            (
                [0] => /Applications/MAMP/tmp/php/phpgBtlBy
                [1] => 
                [2] => 
            )

        [error] => Array
            (
                [0] => 0
                [1] => 4
                [2] => 4
            )

        [size] => Array
            (
                [0] => 75050
                [1] => 0
                [2] => 0
            )

    )

)

推荐答案

MAX_FILE_SIZE 是以字节为单位的值

MAX_FILE_SIZE is a value in Bytes

5120000 不是 500 MB.我估计是5MB.

5120000 is not 500 MB. It's 5MB by my reckoning.

您还需要检查您没有超过 php.ini 文件中的post_max_size"和upload_max_size"变量

You'll also need to check that you're not exceeding the "post_max_size" and "upload_max_size" variables in your php.ini file

其次,mp3 可以是以下任何一种 mimetypes

Secondly, an mp3 can be any of the following mimetypes

  • 音频/mpeg
  • 音频/x-mpeg
  • 音频/mp3
  • 音频/x-mp3
  • 音频/mpeg3
  • 音频/x-mpeg3
  • 音频/mpg
  • 音频/x-mpg
  • 音频/x-mpegaudio

http://fileext.com/file-extension/MP3

相关文章