I'm trying to add an audio player for a file sharing website but I don't know how to bring the player on just for the .mp3 files not for the others.
I do have for .jpeg and others images this function
function is_image($imgPath) {
if (filesize($imgPath) > 10*1024*1024) return false; //to big to handle gd reduction
if (!filesize($imgPath)) return false; //zero byte files
if (function_exists("finfo_file")) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$fileMimeType = finfo_file($finfo, $imgPath);
finfo_close($finfo);
if (!preg_match('/^image\//', $fileMimeType)) {
return false;
}
}
list($w, $h) = getimagesize($imgPath);
if (!$w || !$h) return false;
return true;
}
Any chance to do the same for mp3 ?
My Answer:
You can achieve a similar function for checking if a file is an mp3 file by using the following code:
javascript
function is_mp3($filePath) {
if (filesize($filePath) > 10*1024*1024) return false; //to big to handle
if (!filesize($filePath)) return false; //zero byte files
if (function_exists("finfo_file")) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$fileMimeType = finfo_file($finfo, $filePath);
finfo_close($finfo);
if ($fileMimeType !== 'audio/mpeg') {
return false;
}
}
return true;
}
This function checks the file size, if it is not zero bytes, and if the MIME type is 'audio/mpeg' which is the MIME type for mp3 files. You can use this function to determine if a file is an mp3 file before displaying the audio player for it.
Rate this post
5 of 5 based on 2497 votesComments