简体   繁体   中英

PHP get access to folder previous from file

I am trying to find the previous folder from the file being executed.

For example, I have a file index.php in the folder function. In the main folder, I have a file called option.php and I need to access it from index.php in the function folder.

I have tried require_once '../option.php' but it is breaking when I am using curl so I tried instead the function :

$dir = dirname(__FILE__);

but then I am in the folder of the file in the function folder and don't know how to get in the previous folder. Also, I need to make sure this is portable on other servers so that in case I put the whole program in another folder, it won't break.

Thanks if you can help.

So you've got:

/path/to/your/scripts/function/index.php
/path/to/your/scripts/option.php

If ../option.php isn't working in your index.php script, then you should check what getcwd() says. ../ is a relative path, but it's relative to the (C)urrent (W)orking (D)irectory, which can be something completely different from /path/to/your/scripts/function .

If you want to use relative paths, then you need to be sure of where you're being relative to. eg it's like saying "the treasure is buried 10 meters north". Well, 10 meters north of where?

您应该使用:

$dir = dirname(dirname('/some/path/to/file.ext'));

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