简体   繁体   中英

Php Get file from root dir to another dir

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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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