2013-01-06 157 views
0

考虑这样的工作流程:PHP:搜索目录树的文件,并返回文件路径

用户对website.com/lolmyblogpost

我.htacces是所有喜欢的请求......

RewriteCond %{REQUEST_URI} !=/index.php 
RewriteRule .* /index.php 

在哪里index.php即时查找模板文件树lolmyblogpost.html返回:

/path/to/lolmyblogpost.html 

所以在我的主模板,我可以:

{include file="{$pathToTemplate}"} 

如何搜索目录树的文件,并返回文件路径?

+1

好的。所以你的问题是什么!!!!!!!!!!!!!!! –

+0

@mamdouhalramadan问题是我如何“PHP:文件和返回文件路径的搜索目录树” –

+0

在Apache,PHP,Ruby,...什么语言? – Tigger

回答

1

你真正想要的是“回顾”支持与默认类型。设置你的Apache虚拟主机(东西)是这样的:

<VirtualHost *:80> 
    ServerName example.com:80 
    ServerAlias www.example.com 
    DocumentRoot "/path/to/root/dir" 
    AddDefaultCharset UTF-8 
    ErrorDocument 403 "/403.php" 
    ErrorDocument 404 "/404.php" 
    <Directory /path/to/root/dir> 
      Options Indexes FollowSymLinks 
      DefaultType application/x-httpd-php 
      AllowOverride All 
      Order deny,allow 
      Allow from all 
      AddOutputFilterByType DEFLATE application/javascript text/css text/html text/plain text/xml 
      RewriteEngine On 
      RewriteBase/
      RewriteCond %{REQUEST_FILENAME} !.index.ph.* 
      RewriteRule ^(.*)$ /index.php 
    </Directory> 
</VirtualHost> 

的重要行是DefaultType application/x-httpd-php,这意味着你现在可以摆脱.php文件扩展名。

您现在可以使用一个URL,如http://example.com/this_is_a_php_page,您也可以使用http://example.com/this_is_a_php_page/with_a_path_info_var

因此,在this_is_a_php_page(这实际上是一个没有扩展名的.php文件),您可以使用$_SERVER['PATH_INFO']来检查在URI中传递的变量和变量。

编辑: 添加了RewriteEngine和规则,推动一切index.php。这意味着您现在在服务器上有一个名为index.php的(实际)单个页面,它将需要检查$_SERVER['REDIRECT_URL'] var的真实请求。

例如,对http://example.com/a_page的请求现在将加载index.php并将a_page传递给$_SERVER['REDIRECT_URL']。注意:这个解决方案会将所有内容都推送到index.php。您将需要包括像一个例外:

RewriteCond %{REQUEST_FILENAME} !.not_me_plz.* 

要允许与not_me_plz文件开头的文件送达“预期”。

+0

如果我试图保留'example.com/singlepathtofile',该怎么办?想象一下,在文档根目录下,我的文件树可能看起来像“2012/08,09,10/singlepathtofile”。html' ...'2013/01/singlepathtofile.html,02,03'等 –

+0

作出更新以回答您的问题。 – Tigger

+0

这部分'2012/08,09,10 /'是非常混乱(很难处理)。为什么不有一个像这样的URI:'http:// example.com/title_of_post/2012/08,09,10'或'http:// example.com/2012/08,09,10/title_of_post'这是很容易的处理。 – Tigger

相关问题