通过套接字读取wav文件而不会损坏PHP并将其回显给javascript来播放它

I am trying to play a WAV file I read it through a TCP Socket The file is read and everything going well but doesn't play. Through researching I knew there are problem with reading WAV files in PHP.

How can I read it from the socket without being corrupted?

I am using Joomla and the php class echo the data to Javascript to play the WAV file.

This is my PHP:

public function myfiles() {

    $store = JRequest::getCmd('st');
    $employee = JRequest::getCmd('emp');
    $myfile = JRequest::getCmd('file');
    $q = 'th/'.$store.'/'.$employee.'/'.$myfile;

    /* Get the port for the WWW service. */
    $service_port = 4040;

    /* Get the IP address for the target host. */
    $address = 127.0.0.1;

    /* Create a TCP/IP socket. */
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if ($sock === false) {
        return "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "
";
    }

    $result = socket_connect($socket, $address, $service_port);
    if ($result === false) {
        echo "socket_connect() failed.
Reason: ($result) " . socket_strerror(socket_last_error($socket)) . "
";
    } 

    $in = "file ".$q."
";
    $out = '';

    socket_write($socket, $in, strlen($in));

    while($buf = socket_read($socket, 2048, MSG_WAITALL)) {
        echo $buf;
    }

    $app = &JFactory::getApplication();
    $app->close();

    socket_close($socket);
}

And this is my Javascript

<script>
    $(document).ready(function(){$('#link').click(function(){
        $.post("http://127.0.0.1:4040/cognitechTim/index.php?option=com_timhorton&task=myfiles&st=key1&emp=key2&file=value1", function(data, status){
            var myblob = new Blob([data], { type: "audio/wav" });
            var objectUrl = URL.createObjectURL(myblob);
            var audio = document.getElementById("id2");
            audio.src = objectUrl;
            // Release resource when its loaded
            audio.onload = function(data) {
                URL.revokeObjectURL(objectUrl);
            };
            audio.play();
        });
    }); 
    });
</script>

Thank you for the help.