Skip to content

Instantly share code, notes, and snippets.

@Kaiyuan
Last active December 23, 2015 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kaiyuan/6592634 to your computer and use it in GitHub Desktop.
Save Kaiyuan/6592634 to your computer and use it in GitHub Desktop.
简易防盗链
<?php
$name = $_GET['n']; //获取文件名
$Extension = substr(strrchr($name, "."), 1); //获取格式名
$Pictures = array("png","jpg","jpeg","gif","svg");
$Audios = array("mp3","m4v","m4a","wav","mpa");
$Videos = array("mp4","mov","ogv","ogg","webm");
if (in_array($Extension,$Videos)) { //判断格式设置对应文件夹
$Folder = "videos/";
} else if(in_array($Extension,$Audios)) {
$Folder = "audios/";
} else if($Extension === "zip") {
$Folder = "archive/";
} else if(in_array($Extension,$Pictures)) {
$Folder = "images/";
} else {
# code...
}
$url = $Folder.$name.".".$Extension; //完整路径
$save_name = "file.".$Extension; //保存的文件名
// 检查文件是否存在
if(file_exists($url))
{
$file = @fopen($url,"r"); // 打开文件
$file_data = fread($file,filesize($url));
fclose($file);
// 输入文件标签
Header("Accept-Length: ".filesize($url));
if (in_array($Extension,$Videos)) { //声明文件头
Header("Content-type: video/".$Extension);
} else if (in_array($Extension,$Audios)) {
Header("Content-type: audio/".$Extension);
} else {
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Content-Disposition: attachment; filename=".$save_name);
}
print_r($file_data);
exit;
} else {
header('HTTP/1.1 404 Not Found');
header("status: 404 Not Found");
Header("location: ../404.shtml");
exit;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment