Including files by relative path

2020-02-14 10:28发布

There must be something i don't understand about linking files -- but here's my problem.

Basically, i have three files.

  1. generalfunctions.php
  2. wordcount.php
  3. index.php

All three files are in different directories.

B relies on A as follows: ../../../phpfunctions/generalfucntions.php
And when I run B all is well.

But C relies on B as follows: ../wordcount.php

When I run C, I get an error saying that linked file A cannot be found.

The actual error is:

No such file or directory in /.../public_html/plaoul/text/statistics/wordcount.php on line 11

Any ideas what I'm doing wrong??

Thanks for your help.

标签: php path include
4条回答
够拽才男人
2楼-- · 2020-02-14 10:38

When you use include to include a file, and you use a relative path as parameter, it will be relative to the current working path.

What is the current path? It is normally the path of the first called PHP script. The script where the whole execution started. You can get the current working dir with the function getcwd. For example: <?php echo getcwd(); ?> will show you the current working path. You can change the current working path using the chdir function. For example: <?php chdir( '/home/myself' ); ?> - with this command you just changed the current working path!

So, it is not always good to use relative paths in include, because the current path MAY change.

But with the usage of the __FILE__ magic constant you can use a sort of relative path as a parameter for an include, making it relative to the file where the include command is. This is good! Because no matter what the current working path is, the include will be always relative to the file which is including!

So... try the following:

In B you should include A as follows:

include( dirname( __FILE__ ) . '/../../../phpfunctions/generalfunctions.php' );

In C you should include B as follows:

include( dirname( __FILE__ ) . '/../wordcount.php' );

In short: using dirname( __FILE__ ) you can include the file using a path relative to the file where the "include" command is. Otherwise, the path will be relative to the current working path.

查看更多
Rolldiameter
3楼-- · 2020-02-14 10:38

I've going to take the mathematical approach. PHP always includes additional files from the current working directory, not the current file being included.

So if we are in file C (index.php), we are on level 4. File C includes file B which is in the parent directory (according to the ../) which will be on level 3.

Now file B is included inside file C (with extra emphasis on inside). We are still working from level 4, not level 3. So when you attempt to include file A, we're technically include file A inside file C, not inside file B, so the path is only going up 3 directories instead of the 4 directories it needs to go up through.

Thus when we are executing file B, which is on level 3, include file A will go up three levels to level 0, which is the appropriate place to look for the file.

I'd suggest using absolute paths to access all files because using relative paths only causes problems, such as the one you have found yourself in here.

The way I do it is set a 'root' variable that is accessible throughout all my scripts, for use to define absolute paths. I put it in my index.php file since that is the only file that is ever run.

$ani->i->root = dirname(str_replace("\\", "/",  __FILE__));
// Where my $ani variable is accessible everywhere.

This way whenever I need to include/require files I can simply do something like include "{$ani->i->root}/modules/somemodule.php" and not have to worry about paths, etc.

查看更多
戒情不戒烟
4楼-- · 2020-02-14 10:42

Try to use absolute one instead

include("/public_html/phpfunctions/generalfunctions.php");
查看更多
倾城 Initia
5楼-- · 2020-02-14 10:43

What happens with include() in php is the function is replaced in the file with the included file. You are saying with ../wordcount.php that the file can be found in the parent directory of the current working directory. If you get the path correct for file B, php will replace the function in file b with the contents of file a, then replace the include function in file C with file A(after the include).

Edit:oops, my mistake.

查看更多
登录 后发表回答