Checking download of mime file using Laravel 4

When I download a well-formed MP3 file, Laravel 4 tells me that it is not audio/mp3, but application/octet-streamthat makes this check unsuccessful:

$validator = Validator::make(
    array('trackfile' => Input::file('trackfile')), 
    array('trackfile' => 'required|mimes:mp3')
);

if($validator->fails())
    return 'doesn\'t works because mime type is '.Input::file('trackfile')->getMimeType();
else
    return 'it works!';

Why doesn’t it upload the file as a file audio/mp3?

(I already added 'files' => trueto the form declaration)

+4
source share
2 answers

Edit

The built-in validator still considered some mp3s as an application / octet stream, so you converted the source file with one or another software.

, MimeReader Shane, . , mime, .

Laravel:

  • MimeReader.phps app/libraries/MimeReader.php ( )
  • Laravel Validator ( BaseController)

    Validator::extend('audio', function($attribute, $value, $parameters)
    {
        $allowed = array('audio/mpeg', 'application/ogg', 'audio/wave', 'audio/aiff');
        $mime = new MimeReader($value->getRealPath());
        return in_array($mime->get_type(), $allowed);
    });
    
  • app/lang/[whatever]/validation.php ( audio)
  • audio ! : 'file' => 'required|audio', file Input::file('file')

Laravel audio/mpeg .mpga, .mp3. MimeTypeExtensionGuesser.php ( Symfony) audio/ogg, .oga. , mime , .

- mime , Input::file('upload')->getMimeType(), .

+9

, - mp3 mime audio/mpeg, , mime_content_type application/octet-stream / . ( ) ( /), audio/mpeg - mime mp3 rfc3003:

array('trackfile' => 'required|mimes:audio/mpeg,mp3')

, mime, - :

$file = Input::file('upload');
if($file->getMimeType() == 'audio/mpeg') {
    // valid
}
else {
    // invalid
}

Update:

, mp3 getID3, Php PECL extension id3, , ID3 tag , ​​ title, artist, album, year, genre ..

, (getid3) app, app/libs/ getid3 libs, composer.json classmap autoload, :

// ...
"app/tests/TestCase.php",
"app/libs/getid3"

composer dump-autoload . , :

$file = Input::file('upload'); // assumed name of the file input is upload
$path = $path = $file->getRealPath();
$id3 = new getID3();
$tags = $id3->analyze($path);
if(is_array($tags) && array_key_exists('audio', $tags)) {
    // valid
    dd($tag['tags']);
}

mp3 :

array (size=2)   'id3v1' => 
    array (size=6)
      'title' => 
        array (size=1)
          0 => string '46. Piya Basanti' (length=16)
      'artist' => 
        array (size=1)
          0 => string '(Freshmaza.com)' (length=15)
      'album' =>
      ...

PECL, ( ) :

pear install pecl/id3

$tags = id3_get_tag( "path/to/example.mp3" );
print_r($tag);

:

Array
(
    [title] => DN-38416
    [artist] => Re:\Legion
    [album] => Reflections
    [year] => 2004
    [genre] => 19
)

Php.

+5

Source: https://habr.com/ru/post/1525313/


All Articles