Php 从根目录获取文件到另一个目录
Php Get file from root dir to another dir
嗨,我是 php 编程和学习这个的新手,我发现了一个小问题。
我有这样的目录:
c:/xampp/htdocs/
-practise
+ /css
+ /js
+ /images
- /extra
- /folder1
- / folder1_1
tst.php
index.php
navbar.php
about.php
blab.php
foo.php
lib.php
我创建了一个 lib.php,此文件中包含 /css and /js
(jquery、w3.css 等)的所有文件。我将此文件添加到 tst.php
中,就像这样 include(../../../lib.php);
。当我 运行 我的 tst.php 文件在浏览器中时,lib.php 的内容执行但 css and js
的文件不加载在浏览器上(在检查元素 --> 控制台给我错误找不到文件).
如何在 tst.php 和几乎每个文件夹中使用我的 lib.php...?
我会使用类似 $_server['something']./lib.php...
的东西吗?
这是我的 lib.php
:
echo '<script src="js/browjs.js"></script> ';
echo '<link rel="stylesheet" type="text/css" href="css/poperi.css">';
echo '<link rel="stylesheet" href="css/w3.css">';
echo '<link rel="stylesheet" href="css/navigt.css">';
echo " this is content for check if lib.php is loaded or not";// this line show me in tst.php
我已尽力解释我的问题,但我不知道您还需要了解更多有关此问题的信息...
提前TY...
你可以试试
define( '_LIB_FILE_', $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'practise' . DIRECTORY_SEPARATOR . 'lib.php' );
并使用 _LIB_FILE_
常量 include_one _LIB_FILE_;
$_SERVER['DOCUMENT_ROOT']
是你的根目录 c:/xampp/htdocs/
你只需将你的子目录附加到它
乐:
所以在你的 lib.php 中放入这些代码行
<?php
$root = str_replace( '\', '/', $_SERVER['DOCUMENT_ROOT'] );
$current_dir = str_replace( '\', '/', dirname( __FILE__ ) );
$http_root = 'http://' . $_SERVER['HTTP_HOST'] . str_replace( $root, '', $current_dir ) . '/';
// echo $http_root; // this will let you see what is your current http path of lib.php ex. http://localhost/practise/
// next you include your code
// BEST PRACTICE short for multiple echos
echo '<script src="', $http_root, 'js/browjs.js"></script> ';
// you could do it with concatanation
echo '<link rel="stylesheet" type="text/css" href="' . $http_root . 'css/poperi.css">';
// string evaluation
echo "<link rel='stylesheet' href='{$http_root}css/w3.css'>";
// string evaluation with character escaping \"
echo "<link rel=\"stylesheet\" href=\"$http_rootcss/navigt.css\">";
echo " this is content for check if lib.php is loaded or not";
在您的 tst.php 中,您现在可以包含前面提到的代码段,但我将其转换为变量
// this is called absolute path
$library_file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'practise' . DIRECTORY_SEPARATOR . 'lib.php';
include $library_file;
// or
// include( $library_file );
// and this is relative path. meaning the file relatively to your current file
// include '../../../lib.php';
嗨,我是 php 编程和学习这个的新手,我发现了一个小问题。 我有这样的目录:
c:/xampp/htdocs/
-practise
+ /css
+ /js
+ /images
- /extra
- /folder1
- / folder1_1
tst.php
index.php
navbar.php
about.php
blab.php
foo.php
lib.php
我创建了一个 lib.php,此文件中包含 /css and /js
(jquery、w3.css 等)的所有文件。我将此文件添加到 tst.php
中,就像这样 include(../../../lib.php);
。当我 运行 我的 tst.php 文件在浏览器中时,lib.php 的内容执行但 css and js
的文件不加载在浏览器上(在检查元素 --> 控制台给我错误找不到文件).
如何在 tst.php 和几乎每个文件夹中使用我的 lib.php...?
我会使用类似 $_server['something']./lib.php...
的东西吗?
这是我的 lib.php
:
echo '<script src="js/browjs.js"></script> ';
echo '<link rel="stylesheet" type="text/css" href="css/poperi.css">';
echo '<link rel="stylesheet" href="css/w3.css">';
echo '<link rel="stylesheet" href="css/navigt.css">';
echo " this is content for check if lib.php is loaded or not";// this line show me in tst.php
我已尽力解释我的问题,但我不知道您还需要了解更多有关此问题的信息... 提前TY...
你可以试试
define( '_LIB_FILE_', $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'practise' . DIRECTORY_SEPARATOR . 'lib.php' );
并使用 _LIB_FILE_
常量 include_one _LIB_FILE_;
$_SERVER['DOCUMENT_ROOT']
是你的根目录 c:/xampp/htdocs/
你只需将你的子目录附加到它
乐: 所以在你的 lib.php 中放入这些代码行
<?php
$root = str_replace( '\', '/', $_SERVER['DOCUMENT_ROOT'] );
$current_dir = str_replace( '\', '/', dirname( __FILE__ ) );
$http_root = 'http://' . $_SERVER['HTTP_HOST'] . str_replace( $root, '', $current_dir ) . '/';
// echo $http_root; // this will let you see what is your current http path of lib.php ex. http://localhost/practise/
// next you include your code
// BEST PRACTICE short for multiple echos
echo '<script src="', $http_root, 'js/browjs.js"></script> ';
// you could do it with concatanation
echo '<link rel="stylesheet" type="text/css" href="' . $http_root . 'css/poperi.css">';
// string evaluation
echo "<link rel='stylesheet' href='{$http_root}css/w3.css'>";
// string evaluation with character escaping \"
echo "<link rel=\"stylesheet\" href=\"$http_rootcss/navigt.css\">";
echo " this is content for check if lib.php is loaded or not";
在您的 tst.php 中,您现在可以包含前面提到的代码段,但我将其转换为变量
// this is called absolute path
$library_file = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'practise' . DIRECTORY_SEPARATOR . 'lib.php';
include $library_file;
// or
// include( $library_file );
// and this is relative path. meaning the file relatively to your current file
// include '../../../lib.php';