Skip to content
Advertisement

I’m trying to add an audio player just for mp3 files but really stuck on it

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 ?

Advertisement

Answer

Here is a function to know if the past file is an mp3 :

function is_mp3($file_path){
    
    $mime_type = mime_content_type($file_path);
    
    // List of allowed file mime type

    $allowed_mime_type = [
        'audio/mp3',
        'audio/mpeg',
        'audio/x-mpeg',
        'audio/mpeg3', 
        'audio/x-mpeg-3'
    ];
    
    if(in_array($mime_type, $allowed_mime_type)){
        return TRUE; // Return TRUE if file is MP3
    }else{
        return FALSE; // Return TRUE if file isn't MP3
    }
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement