当前位置: 动力学知识库 > 问答 > 编程问答 >

php - accessing a file in the hierarchy- not working

问题描述:

How can I go out of the currect directory and access the test.php file?

I have tried the following:

realpath(__dir__.'../test.php');

realpath(__dir__.'./test.php');

But it does not work!

Also could you please let me know what is the difference between ./ and ../ ?

UPDATED:

The following lines give the following results:

 echo(__dir__.'\tests.php');

echo "<br/>";

echo(__dir__.'..\tests.php');

echo "<br/>";

echo(realpath(__dir__.'..\tests.php'));

echo "<br/>";

echo "hi";

die();

Result:

C:\xampp\htdocs\proj1\includes\tests.php

C:\xampp\htdocs\proj1\includes..\tests.php

hi

So whay the third one is empty?

Thanks

网友答案:

Just ad another directory separator and perhaps move the desired path . For instance, it's good practice to use the constant:

realpath(__DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "test.php");

Edit:

. stands for "current directory" (has no practical effect in this case)

.. stands for "parent directory" and goes up one level.

网友答案:

One dot (.) means 'current directory', and two dots (..) means 'parent directory'.

Keep in mind that __DIR__ will be the directory that the currently running php file is in. Check with an 'echo __DIR__' first to make sure it's running from the directory you think it is.

分享给朋友:
您可能感兴趣的文章:
随机阅读: