从不同的文件夹访问所有文件


Access all files from separate folders

如何访问文件夹中的所有文件,例如:

我已将我的网站文件添加到文件夹中。在网站的根目录中,我有一个文件夹,其中包括我的文件,index.php和我的主题菜单。所以我的问题是,

如何从菜单中的特定链接访问该文件夹中的文件?

我不希望像"myste.com/sections/forum.php"那样访问它们,而是需要知道如何像"mysite.com?page=forum"或CMS那样访问它们。如果有人能帮我,我真的很感激。我一直在寻找解决方案,但没有成功。

谢谢。

命令mysite.com?page=forum将运行mysite根文件夹中的index.php文件。

因此,您需要在index.php文件中编写一些代码来重定向到正确的位置,如

<?php
if (isset($_GET, $_GET['page'])) {
    // sanitize the $_GET contents
    switch ($_GET[['page']) {
        case 'form' :
           header( 'Location: sections/forum.php' );
           exit;
           break;
        case '...' :
        // etc etc
    }
} else {
    echo 'No $_GET';
}

不幸的是,它并没有到此为止,因为您可能希望在querystring和page上放置其他参数。所以现在你必须决定如何处理这些其他参数。你是将它们添加到header()中,还是将它们存储在其他地方,并确保应用程序的其他部分知道从哪里获取它们。

<?php
if (isset($_GET, $_GET['page'])) {
    // sanitize the $_GET contents
    $gets = $_GET;
    unset($gets['page'];
    $qs = '?' . implode('&',$gets);
    switch ($_GET[['page']) {
        case 'form' :
           header( 'Location: sections/forum.php' . $qs );
           exit;
           break;
        case '...' :
            // etc etc
       }
} else {
    echo 'No $_GET';
}

你有很多方法可以做到这一点,但如果我理解你的问题,一个解决方案可能是:

if (!empty($_GET["page"])) {
   switch ($_GET["page"]) {
        case "forum":
           include('section/forum.php');
           break;
        case "something_else":
           //include other file, or do whatever want
           break;
        default:
           //every else case 
           break;
   }
}

谢谢,是的,有点工作,但在浏览器中,而不是显示mysite.com/index.php?page=论坛显示mysite.com/sections/forum.php

我错过什么了吗?

谢谢。