1

I was making a website in php and I need to upload audio files from users and store them in a MySql database and retrieve when needed.

If we have to use blob then how to check if the file is of audio type or not and how to tetrieve it?

If this won't work please suggest me other ways to do so.

Tim Kathete Stadler
  • 1,067
  • 4
  • 26
  • 52
Strarter
  • 11
  • 2

1 Answers1

2

The simple answer to your question is that you cannot simply check the BLOB to see what file type it is once it's in MySQL. You can analyse the headers of the first few bytes of the blob but that's quite complicated.

The correct way to do this is when a user uploads the file, it's stored in a temporary location and you can access it (and move it to a more sensible place or your database). Check the mime type of the file using fileinfo: http://php.net/manual/en/function.finfo-file.php

Then store the mime-type in a new column alongside the blob.

Update: If you are using PHP < 5.3.0 then this solution allows you to check the mimetype using an older (now deprecated) function: https://stackoverflow.com/a/8225754/775007

Community
  • 1
  • 1
Dean
  • 5,884
  • 2
  • 18
  • 24