您的位置:首页 >PHP 判断本地文件是否存在的方法
发布于2026-04-09 阅读(0)
扫一扫,手机访问

本文讲解为何不应使用 get_headers() 检查当前站点内文件存在性,以及如何通过 file_exists() 安全、高效地实现本地资源路由分发,避免递归请求、超时和 404 警告。
本文讲解为何不应使用 `get_headers()` 检查当前站点内文件存在性,以及如何通过 `file_exists()` 安全、高效地实现本地资源路由分发,避免递归请求、超时和 404 警告。
你在 index.php 中尝试用 get_headers($url) 判断一个形如 http://example.com/assets/style.css 的 URL 是否存在,本质上是在发起一次新的 HTTP 请求回环到自身服务器——这不仅造成性能浪费,更会因未正确处理重定向或超时而触发 failed to open stream: HTTP request failed 警告,甚至引发无限递归(例如当 index.php 自身被 get_headers() 请求时再次执行路由逻辑)。
✅ 正确做法是:将 URL 路径映射为服务器本地文件系统路径,再用 file_exists() 判断。这是零网络开销、毫秒级响应、完全可控的安全方案。
假设你的项目结构如下:
/project-root/ ├── index.php ← 入口路由 ├── assets/ │ ├── style.css │ └── logo.png ├── views/ │ └── dashboard.php └── public/ ← 可选:实际 Web 根目录(若配置了 DocumentRoot)
在 index.php 中,你需要:
<?php
// index.php
// 1. 定义允许被直接访问的静态资源目录(相对于 index.php 所在目录)
$public_dirs = [
'assets' => __DIR__ . '/assets',
'images' => __DIR__ . '/images',
'js' => __DIR__ . '/js',
];
// 2. 获取干净的请求路径(移除 query string 和 fragment)
$request_uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$request_uri = rtrim($request_uri, '/');
// 3. 防止路径遍历:拒绝含 '..' 或以 '/' 开头的非法路径
if (strpos($request_uri, '..') !== false || $request_uri[0] === '/') {
http_response_code(403);
die('Forbidden');
}
// 4. 尝试匹配静态资源路径(如 /assets/style.css)
foreach ($public_dirs as $uri_prefix => $real_path) {
if (str_starts_with($request_uri, "/{$uri_prefix}/")) {
$file_path = $real_path . substr($request_uri, strlen("/{$uri_prefix}"));
// 安全校验:确保解析后路径仍在 $real_path 内
if (realpath($file_path) === false ||
strpos(realpath($file_path), realpath($real_path)) !== 0) {
http_response_code(403);
die('Access denied');
}
if (file_exists($file_path)) {
// 设置合适的 Content-Type(简易版,生产环境建议用 mime_content_type 或扩展映射)
$ext = pathinfo($file_path, PATHINFO_EXTENSION);
$mime_types = [
'css' => 'text/css',
'js' => 'application/javascript',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'svg' => 'image/svg+xml',
];
$mime = $mime_types[$ext] ?? 'application/octet-stream';
header("Content-Type: {$mime}");
readfile($file_path);
exit;
}
}
}
// 5. 若未命中静态资源,则进入动态路由逻辑(如 MVC 分发)
// include __DIR__ . '/router.php';
// 或渲染视图:
// if (file_exists(__DIR__ . '/views' . $request_uri . '.php')) {
// include __DIR__ . '/views' . $request_uri . '.php';
// } else {
// http_response_code(404);
// include __DIR__ . '/404.php';
// }用 file_exists(__DIR__ . $mapped_path) 替代 get_headers($full_url),是构建健壮、高性能原生 PHP 路由的核心实践之一。它消除了网络延迟、规避了递归风险、提升了安全性,并为后续实现缓存、压缩、版本化资源等高级功能打下坚实基础。
下一篇:《刀鸣》金币获取方法及技巧分享
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
9