以下是一个简单的 PHP 镜像站示例代码:
<?php
// 获取参数
$url = isset($_GET["url"]) ? $_GET["url"] : "";
if (!$url) {
echo "请输入要获取的 URL";
exit;
}
// 获取远程文件内容
$content = file_get_contents($url);
if (!$content) {
echo "获取内容失败";
exit;
}
// 输出内容
header("Content-Type: " . get_mime_type($url));
echo $content;
// 获取 MIME 类型
function get_mime_type($url) {
$mime_types = array(
"txt" => "text/plain",
"html" => "text/html",
"php" => "text/html",
"css" => "text/css",
"js" => "application/javascript",
"json" => "application/json",
"xml" => "application/xml",
"swf" => "application/x-shockwave-flash",
"flv" => "video/x-flv",
"png" => "image/png",
"jpe" => "image/jpeg",
"jpeg" => "image/jpeg",
"jpg" => "image/jpeg",
"gif" => "image/gif",
"bmp" => "image/bmp",
"ico" => "image/vnd.microsoft.icon",
"tiff" => "image/tiff",
"tif" => "image/tiff",
"svg" => "image/svg+xml",
"svgz" => "image/svg+xml"
);
$ext = pathinfo($url, PATHINFO_EXTENSION);
return isset($mime_types[$ext]) ? $mime_types[$ext] : "application/octet-stream";
}
?>
此示例代码可以通过 GET 请求获取指定 URL 的内容,并输出该内容的 MIME 类型。您可以将此代码上传至您的 PHP 主机或服务器,并访问该文件来获取远程文件的内容。