简体   繁体   中英

Dynamically Include Files From Different Folders

I have the following script to do dynamic includes:

 <?php   
$_GET["page"] = (isset($_GET["page"])) ? $_GET["page"] : "home";

    $page = $_GET['page'];
    $pages = array('home', 'solutions', 'projects', 'about', 'contact');
    if (!empty($page)) {
        if(in_array($page,$pages)) {
            $page .= '.php';
            include($page);
        }
        else {
        echo 'Page not found. Return to
        <a href="index.php">index</a>';
        }
    }
        else {
        include("home.php");
    }
       ?>

What should I add to this script to be able to also include DYNAMICALLY files from different folders (eg I have an index.php page that is in the www/mysite_folder and I want to dynamically include into the index.php the file Adresses.php that is in www/mysite_folder/faq_folder )? Note that by dynamic I don't mean the regular include ( include(); ) but <a href="index.php?category=faq_folder&page=adresses">Adresses</a> .

I see no reason for splitting executable files between directories.
So, if you can keep all the files in one, here is the code

<? 
$page = (empty($_GET["page"])) ? "home" : $_GET["page"];
$page = "includes/".basename($page).".php"; 
if (is_readable($page)) { 
  include($page); 
} else {
  header("HTTP/1.0 404 Not Found"); 
  exit;  
} 
?>

which is secure and automated.

require('faq_folder/'.$file_name); But it is not very secured solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM