Answer the question
In order to leave comments, you need to log in
php. How to check an mp3 file for validity?
Hello!
I have the following task: I need to upload files to the server by dragging and dropping files into the browser window in a certain area. As an additional condition, only mp3 audio files should be uploaded. In general, everything is done and works.
I just thought that you can rename any NOTaudio file to mp3 and upload it to the server, which is not good. On the one hand, it will not be played, but on the other hand, who knows what will be loaded there? Actually, the question is precisely this: how to check an mp3 file that it is really an audio file and not upload everything else to the server that can slip under the guise of music? On the client, only the file format (mp3) is checked via javascript. A more detailed check, I think, should be done on the server. So far, the server-side script looks like this:
<?php
$uploaddir = getcwd().DIRECTORY_SEPARATOR.'upload'.DIRECTORY_SEPARATOR;
$uploadfile = $uploaddir.basename($_FILES['file']['name']);
if($_FILES["file"]["type"]=="audio/mp3"){
move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile);
}
?>
Answer the question
In order to leave comments, you need to log in
Use any MP3 class and check bitrate, duration, etc. If everything is correct, then this is a real MP3.
For example, here is one of the classes - www.zend.com//code/codex.php?ozid=160&single=1
In addition (if you need to work not only with mp3), such a getid3.org combine will be useful
If it is possible to install PHP extensions on the server, then, in order not to reinvent the wheel, I would recommend the id3: php.net PECL extension .
It seems to me that this will be less resource intensive and faster than any classes written in PHP.
To check, in my opinion, the easiest way is:
$version = id3_get_version($filename);
if (!$version)
die('Не аудиофайл!');
Give a link to the service, load php (if the form is apload, as above) :)
As an option - checking some magic values, it is not necessary to decode the stream itself. By the way, there may simply be no ID3 tags, and if there are, then no extensions are needed, just read the last 128 bytes of the file - the line should begin with the word "TAG"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question