PHP文件下载,第一次显示PDF代码,然后工作

PHP文件下载,第一次显示PDF代码,然后工作,php,pdf,Php,Pdf,我有一个PHP下载脚本,我已经使用了多年,突然我的用户报告说,他们第一次尝试下载文件时,它会显示如下随机PDF脚本: %PDF-1.5%µ1 0 obj>>endobj 2 0 obj 然后下一次,它的工作完美,每一次之后。对可能发生的事情有什么想法吗?以下是下载脚本: <?php include_once("includes/functions.php"); if(!sess('id')){ header("Location: $base_url"); }

我有一个PHP下载脚本,我已经使用了多年,突然我的用户报告说,他们第一次尝试下载文件时,它会显示如下随机PDF脚本:

%PDF-1.5%µ1 0 obj>>endobj 2 0 obj

然后下一次,它的工作完美,每一次之后。对可能发生的事情有什么想法吗?以下是下载脚本:

 <?php

  include_once("includes/functions.php");

  if(!sess('id')){
    header("Location: $base_url");
  }

  if(empty($_REQUEST['id'])){
    header("Location: $base_url");
    exit();
  }

  $showFile = true;

  $id = explode('.',$_REQUEST['id']);
    $identifier = $id[0];
    $date = $id[1];


  // Pull file information
    $db = db();
    $stmt = $db->prepare('SELECT * FROM files WHERE identifier=:identifier AND date=:date');
    $stmt->execute(array(
      ':identifier'=>$identifier,
      ':date'=>$date
    ));
    $r = $stmt->fetch();

  $fileID = $r['file_id'];
  $fileType = $r['type'];


  // Check if user is a student, then see if they can view the file
    if(sess('level')==3){
      if($fileType==0 || $fileType==2){
        $sID = $_SESSION['semester'];
        if(!showFile($sID,2,$fileID)){
          $showFile = false;
        }
      }
    }

  if($showFile){
    $fileName = $r['filename'].".".$r['ext'];

      switch($r['ext']){
        case 'doc':
          $contentType = 'application/msword';
          break;
        case 'docx':
          $contentType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
          break;
        case 'pdf':
          $contentType = 'application/pdf';
          break;
        case 'ppt':
          $contentType = 'application/vnd.ms-powerpoint';
          break;
        case 'pptx':
          $contentType = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
          break;
        case 'xls':
          $contentType = 'application/vnd.ms-excel';
          break;
        case 'xlsx':
          $contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
          break;
      }

    $uniqueID = $r['unique_id'];
    $date = $r['date'];
      $year = date('Y',$date);
      $month = date('m',$date);
      $day = date('d',$date);

    $dir = "uploads/$year/$month/$day/$uniqueID";

    header('Content-Description: File Transfer');
    header('Content-Type: '.$contentType);
    header("Content-Disposition: attachment; filename=\"$fileName\"");
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($dir));
    ob_clean();
    flush();
    readfile($dir);
  } else {
    header("Location: $base_url");
  }
?>

感谢您的帮助,谢谢