dongxing2710 2015-11-09 14:09
浏览 207
已采纳

Php从root目录获取文件到另一个目录

Hi I am new in php programming and learning this i have found a small problem. I Have directories something like this:

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

I have created a lib.php where in this file contain all files of /css and /js(jquery, w3.css,etc). And i add this file in tst.php like this include(../../../lib.php);. when i run my tst.php file in browser, the content of lib.php execute but the files of css and js dont load on browser (in inspect element --> console give me error file not found).

How can i use my lib.php in tst.php and almost in every folder... ?

Do i use something like $_server['something']./lib.php... ?

here is my 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

I tried my best to explain my problem and i dont know what u need more to know about this problem... TY in advance...

  • 写回答

1条回答 默认 最新

  • duanan2732 2015-11-09 14:15
    关注

    you could try

    define( '_LIB_FILE_', $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'practise' . DIRECTORY_SEPARATOR . 'lib.php' );
    

    and use the _LIB_FILE_ constant include_one _LIB_FILE_;

    $_SERVER['DOCUMENT_ROOT'] is your root directory c:/xampp/htdocs/ you just append your subdir to it

    LE: so in your lib.php put these lines of code

    <?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";
    

    in your tst.php you can now include the snippet previously mentioned, but I converted it to variable

    // 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';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python