使用PHP将绝对文件路径从配置文件传递到HTML



我最近刚刚实现了一个新的目录模式,其工作原理如下:

|| Directory Schema ||
>Shape_Search/  
>public/                
>css/       
>img/
>content/
>layout/
>js/
>resources/             
config.php      
>Libraries/     
>Classes/           
>Templates/         
>src/               
>ImageUtilities/
>RatingUtilities/
>UserUtilities
>README.txt

现在所有的文件都不在一个大文件夹中,我需要能够遍历轨迹。我已经尝试过为所有主要文件夹定义绝对路径常数,并将它们放在config.php文件中。我还将一些文件夹的绝对路径存储在多维数组中。

每当我尝试使用这两个存储路径中的任何一个来访问我的项目中的文件时,我都一无所获。以下是我的配置文件和我尝试使用这些方法的index.php文件的摘录。

/*
config.php
*/
$config = array(
"dbCred" => array(
"ai_search" => array(
"hostname" => "localhost",
"username" => "root",
"password" => "",
"dbname" => "ai_search"
)
),
"urls" => array(
"baseUrl" => "http://localhost/Shape_Search/public"
),
"paths" => array(
"images" => array(
"content" => $_SERVER["DOCUMENT_ROOT"] . "/Shape_Search/public/img/content",
"layout" => $_SERVER["DOCUMENT_ROOT"] . "/Shape_Search/public/img/layout"
)
)
);
/*
Create constants for heavily used paths relative to config file location
*/
define("LIBRARY_PATH", realpath(dirname(__FILE__)) . 'Library');
define("CLASSES_PATH", realpath(dirname(__FILE__)) . "Classes");
define("TEMPLATES_PATH", realpath(dirname(__FILE__)) . "Templates");
define("IMAGE_UTIL_PATH", $_SERVER["DOCUMENT_ROOT"] . "/Shape_Search/src/ImageUtilities");
define("RATING_UTIL_PATH", $_SERVER["DOCUMENT_ROOT"] . "/Shape_Search/src/RatingUtilities");
define("USER_UTIL_PATH", $_SERVER["DOCUMENT_ROOT"] . "/Shape_Search/src/UserUtilities");
<p>
<img src="<?php echo $config['paths']['images']['layout'];?> shapes-background.jpg" alt="VARIOUS SHAPES" width="500px">
</p>
<link rel="stylesheet" href="<?php echo LIBRARY_PATH; ?>/fontawesome-free-5.15.4-web/css/all.css?v=51">

我想明白了。非常感谢。

||config.php ||

"paths" => array(
"images" => array(
"content" => realpath($_SERVER["HTTP_HOST"]) . "/Shape_Search/public/img/content",
"layout" => realpath($_SERVER["HTTP_HOST"]) . "/Shape_Search/public/img/layout"
)
)

||index.php ||

require_once ('../resources/config.php');
$backgroundImagePath = $config['paths']['images']['layout'] . '/shapes-background.jpg';
echo $backgroundImagePath;
<p>
<img src="<?php echo $backgroundImagePath;?>" alt="VARIOUS SHAPES" width="500px">
</p>

相关内容

  • 没有找到相关文章

最新更新