isValid()) { $fail('Fail audio tidak sah.'); return; } $handle = @fopen($value->getRealPath(), 'rb'); if ($handle === false) { $fail('Fail audio tidak dapat dibaca.'); return; } $header = fread($handle, 12); fclose($handle); if (strlen($header) < 4) { $fail('Fail audio terlalu kecil atau rosak.'); return; } $bytes = array_values(unpack('C*', $header)); foreach (self::SIGNATURES as $sig) { if ($this->matchesAt($bytes, $sig, 0)) { return; } } // Check MP4/M4A: need at least 8 bytes if (count($bytes) >= self::MP4_OFFSET + count(self::MP4_SIGNATURE)) { if ($this->matchesAt($bytes, self::MP4_SIGNATURE, self::MP4_OFFSET)) { return; } } $fail('Format fail audio tidak sah. Kandungan fail tidak menepati format audio yang dibenarkan.'); } private function matchesAt(array $bytes, array $signature, int $offset): bool { foreach ($signature as $i => $expected) { if (($bytes[$offset + $i] ?? -1) !== $expected) { return false; } } return true; } }