递归查看子目录中的文件名匹配。

时间:2014-08-13 09:55:09

标签: apache .htaccess http mod-rewrite

如果找不到文件,我想以递归方式检入子目录,直到找到具有所请求名称的文件并将其传送出去。

示例:

请求是:

http://domain.com/file.txt

在子目录中递归查找,直到找到file.txt,然后传递:

http://domain.com/foo/bar/file.txt

我到目前为止唯一管理的是找不到文件的触发器:

RewriteCond %{REQUEST_FILENAME} !-f

1 个答案:

答案 0 :(得分:0)

将其转换为加载程序脚本。首先,在您的应用程序中重写(vhost配置或.htaccess文件)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /loader.php?q=$1 [L,QSA]

然后在您网站的根目录中有一个loader.php脚本,它会递归搜索文件并重定向到正确的URL或加载文件,获取mime tipe,设置正确的标题并回复与该文件的内容。像这样:

<?php
// Do some processing here to extract the proper file name
// from $_REQUEST['q'] into a variable called $filename

// Assume we have the $filename
$it = new RecursiveDirectoryIterator(__DIR__); // Start search where loader.php is located
foreach (new RecursiveIteratorIterator($it) as $file) {
    if (pathinfo($file, PATHINFO_BASENAME) == $filename) {
        $returnFile = $file;
        break;
    }
}

// Check if the file was found
// Do a proper redirect to the 404 page here if you need more
if (empty($returnFile)) {
    header ("HTTP/1.0 404 Not Found");
    die();
}

// You need to do some fancy magic here to set the proper content type
// We will return plain text for now
header("Content-Type:text/plain");

// Handle large files
set_time_limit(0);
$file = @fopen($file_path,"rb");
while(!feof($file))
{
    print(@fread($file, 1024*8));
    ob_flush();
    flush();
}

/* EndOfScript */

关于它,我想......我还没有测试过这个,所以可能有很多错误!然后,您可以获得如何处理此问题的主要图片!