我已经使用了多年这个PHP下载脚本,突然我的用户报告说他们第一次尝试下载文件时会显示随机PDF脚本:
%PDF-1.5%μμμμ10obj>> 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");
}
?>
感谢任何帮助,谢谢!