Last active
December 23, 2015 06:18
-
-
Save Kaiyuan/6592634 to your computer and use it in GitHub Desktop.
简易防盗链
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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