简体   繁体   English

PHP包含路径中的变量

[英]PHP include with a variable in the path

So we build websites that have to ported from local development servers, to a test server, then to a live server. 因此,我们构建的网站必须从本地开发服务器移植到测试服务器,然后移植到实时服务器。 For this reason we have created a variable: 出于这个原因,我们创建了一个变量:

<?php $path = '/_Folder_/_SubFolder_'; ?>

When we move the website from server to the next, the idea is to simply change the $path definition to retrofit to the new development server. 当我们将网站从服务器移动到下一个网站时,我们的想法是简单地将$ path定义更改为改造到新的开发服务器。 Currently, on each page when we call an include we write: 目前,在我们调用包含的每个页面上,我们写道:

<?php include('../_includes/_css.php'); ?>

but what I'm trying to do is to: 但我要做的是:

<?php include($path.'/_includes/_css.php'); ?>

my result I hope for is: 我希望的结果是:

<?php include('/_Folder_/_SubFolder_/_includes/_css.php'); ?>

I've been failing miserably resulting in: 我一直在失败,导致:

Warning: include(/ Folder /_SubFolder_/_includes/_css.php) [function.include]: failed to open stream: No such file or directory in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\FREEDOM2012_FREEDOM2012_DEFAULT_\\accommodations\\guest-rooms.php on line 15 警告:include(/ Folder /_SubFolder_/_includes/_css.php)[function.include]:无法打开流:C:\\ Program Files \\ Apache Software Foundation \\ Apache2.2 \\ htdocs \\ FREEDOM2012_FREEDOM2012_DEFAULT_ \\中没有此类文件或目录15号线的住宿\\ guest-rooms.php

Other Issue I still need to include the variable source by calling "../" 其他问题我仍然需要通过调用“../”来包含变量源。

<?php include('../_includes/_var.php'); ?>

if anybody has any insight into how I could do this more efficiently I would be most appreciative. 如果有人对如何更有效地做到这一点有任何见解,我将非常感激。 Thank you very much for your time, patience and effort in responding. 非常感谢您的回复时间,耐心和努力。

Why don't you initailize your $path variable dynamically once forever and environnement-independant ? 为什么不永远动态地启动你的$ path变量,并且环境独立?

like: 喜欢:

$path = dirname(__FILE__);

Starting with PHP 5.3, you have also the __DIR__ shortcut that does the same as above. 从PHP 5.3开始,您还可以使用与上面相同的__DIR__快捷方式。

@see : http://www.php.net/manual/en/language.constants.predefined.php @see: http ://www.php.net/manual/en/language.constants.predefined.php

you need show previous directory from 你需要显示以前的目录

<?php include('../_includes/_css.php'); ?>

becose you have " ../ ". 因为你有“ ../ ”。 If your full path from " css.php " = / _* Folder *_ / SubFolder /_includes/_css.php , then you need use next code: 如果您的完整路径来自“ css.php ”= / _ * Folder * _ / SubFolder /_includes/_css.php,那么您需要使用下一个代码:

<?php $path = './_Folder_/_SubFolder_';
include($path.'/_includes/_css.php'); ?>

else first show full path to file _css.php 否则首先显示文件_css.php的完整路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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