PHP - 服务文件出错

Here is my code. I wrote this to serve mp3, images and videos. Mp3 files are received and working well. Images and videos are received but corrupted. I'm new to php.

<?php
if( !empty( $_GET['type']|| $_GET['name']|| $_GET['ext'] ) ) {
  // check if user is logged
  if(true) {
    $type = preg_replace( '#[^-\w]#', '', $_GET['type'] );
    $name = $_GET['name'] ;
    $file = "{$_SERVER['DOCUMENT_ROOT']}/cont/{$type}/{$name}";
    echo $file;

    if (file_exists($file)) {
      header('Content-Description: File Transfer');
      header('Content-Type: application/octet-stream');
      header('Content-Disposition: attachment; filename="'.basename($file).'"');
      header('Expires: 0');
      header('Cache-Control: must-revalidate');
      header('Pragma: public');
      header('Content-Length: ' . filesize($file));
      readfile($file);
      exit;
    }
  }
}
die( "ERROR: invalid file or you don't have permissions to download it." );

Can you help me?

There is an echo $file; before the actual response that doesn't belong there. Besides the text itself, PHP also produces a Warning on the first call to header(). Both these arrive in the final content before the file header and makes the data received by the browser unrecognizable by most file readers.