简体   繁体   English

启用在php中包含父文件夹的文件

[英]enable to include parent folder's file in php

The answer is already given but it is not satisfied. 答案已经给出,但不满意。 This is my directory structure and I am trying to include the header.php in index.php 这是我的目录结构,我试图将header.php包含在index.php

-my_app
   -assets
     -js
       -my_app.js
     -css
       -my_app.css

   -includes
    -header.php

   -index.php
-test
  -my_test.php 

I tried with include '../includes/header.php'; 我试着用include '../includes/header.php'; but is not working.I also want to include my_app.js file in index.php file.How can I do that? 但没有用。我也想在index.php文件中包含my_app.js文件,该怎么办?

Try with - 尝试-

include 'includes/header.php';

includes and index.php are in same folder. includesindex.php在同一文件夹中。

The path will be - ../assets/css/my_app.css 路径将是- ../assets/css/my_app.css

You just need to do include 'includes/header.php'; 您只需要include 'includes/header.php'; , but I would recommend doing something like include __DIR__ . '/includes/header.php'; ,但我建议您执行诸如include __DIR__ . '/includes/header.php'; include __DIR__ . '/includes/header.php'; .
For my_app.js , use this: include 'assets/js/my_app.js'; 对于my_app.js ,请使用: include 'assets/js/my_app.js'; .

include 'includes/header.php';

include 'assets/js/my_app.js'; 

Basically in all operating systems: 基本上在所有操作系统中:

  1. to call/include neghbour files and direcotories, simply call the their names 调用/包含邻居文件和目录,只需调用它们的名称
  2. to call/include the files and direcotories resideing in the neighbour directories, proceed with the hirearchical approach 要调用/包括邻居目录中的文件和目录,请继续使用hirarchical方法
  3. to call/include files that are in the upper levels, for each uplevel you need to use one ../ to access to a higher level direcotory and then go on with the process in the second item 调用/包含较高级别的文件,对于每个较高级别,您需要使用一个../来访问较高级别的目录,然后继续执行第二项中的过程

Im Sure you need your header.php, so dont use include (You got no error and no script-break if the include fails. Also im sure you need the header only once ;) 我确定您需要header.php,所以请不要使用include(如果include失败,您不会出错,也不会破坏脚本。还要确保只需要一次header;)

require_once('/includes/header.php');

for CSS/JS use sinply ../ to go one step higher :) 对于CSS / JS,请使用sinply ../向前迈进一步:)

Br

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

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