D
D
Dmitry Petrik2013-03-22 06:35:57
PHP
Dmitry Petrik, 2013-03-22 06:35:57

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);
  }
?>

If, apart from the solution of the problem, give some more practical advice on this task, I will be grateful :)

Answer the question

In order to leave comments, you need to log in

7 answer(s)
R
RuslanCC, 2013-03-22
@RuslanCC

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

A
Anatol Pohorilyi, 2013-03-22
@decameron

In addition (if you need to work not only with mp3), such a getid3.org combine will be useful

D
Dzuba, 2013-03-22
@Dzuba

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('Не аудиофайл!');

S
Sergey Belov, 2013-03-22
@BeLove

Give a link to the service, load php (if the form is apload, as above) :)

M
makkarpov, 2013-03-22
@makkarpov

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"

F
Fyodor, 2013-03-22
@Richard_Ferlow

Why isn't the extension checked on the server?

E
Elkan, 2013-03-23
@Elkan

The usual installed www.php.net/manual/en/book.fileinfo.php will suffice.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question