【问题标题】:Php Get file from root dir to another dirPhp 从根目录获取文件到另一个目录
【发布时间】:2015-11-09 14:09:12
【问题描述】:

您好,我是 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...

【问题讨论】:

  • 试试 echo '' ;
  • 如果我将我的网站上传到 altervista 所有文件 from 练习文件夹,那么我是否必须更改所有代码??

标签: php dir home-directory


【解决方案1】:

你可以试试

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 中包含前面提到的 sn-p,但我将它转换为变量

// 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';

【讨论】:

  • 您好,先生回答,但我怎么说我是新的编程语言,我还不知道如何使用定义……你能为我提供另一个更简单、更舒适的解决方案吗……跨度>
  • 我编辑了我的答案,所以也许你可以更好地理解需要做什么。还要记住 $root 和 $current_dir 取决于您的操作系统。 windows有反斜杠\而其余(我认为)普通斜杠/。所以赢了它实际上是 c:\xampp\htdocs\ 但它也适用于斜线。 DIRECTORY_SEPARATOR,顾名思义就是你操作系统的目录分隔符
  • 如果我将其上传到主机上,我是否必须更改某些内容,或者我不会?
  • 如果您将使用绝对路径而不是是 $library_file = $_SERVER['DOCUMENT_ROOT'] 。目录分隔符。 '实践' 。目录分隔符。 'lib.php';,但是如果您将使用相对路径来包含文件而不是不包含,因为 str_replace 会为您处理它。在 lib.php 中,您无需像包含 lib.php 那样更改任何内容,因此通过使用相对路径 ../../../../lib.php 您无需更改任何内容
  • 这些绝对路径和相对路径是什么?是绝对路径(htdocs/practise/extra/folder1/folder1_1)还是(localhost/practice/extra/folder1/folder1_1/tst.php)?是相对路径(extra/folder1/folder1_1)?
猜你喜欢
  • 2021-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-27
  • 2017-07-20
  • 2012-02-15
  • 1970-01-01
相关资源
最近更新 更多