Asked  7 Months ago    Answers:  5   Viewed   49 times

If I have an index.php file that includes inc/footer.php I would write:

include 'inc/footer.php';

If I want to include another file inside footer.php, I must do it relative to the index.php file (the one that is including it). This may not be a problem, but what about if I want to include index.php from an entire different location?

I understand that there are several methods to achieve this like defining an absolute path or using dirname(__FILE__).

This is something that has never been a real problem since one way or another I always figured it out but that I always wondered how exactly includes work in php.

Can someone explain me exaclty what is going on under the hood?

 Answers

74

This may help: (from http://php.net/manual/en/function.include.php)

Files for including are first looked for in each include_path entry relative to the current working directory, and then in the directory of current script. E.g. if your include_path is libraries, current working directory is /www/, you included include/a.php and there is include "b.php" in that file, b.php is first looked in /www/libraries/ and then in /www/include/. If filename begins with ./ or ../, it is looked for only in the current working directory or parent of the current working directory, respectively

Your question states:

If I want to include another file inside footer.php, I must do it relative to the index.php file (the one that is including it).

This is true only if the filepath you are trying to include() starts with ./ or ../ . If you need to include a file above the current file using a relative path, you can (as you suggested) use:

include( dirname(__FILE__) . '/../file.php')

If you define an absolute path, you can also add this to the current include_path:

set_include_path(get_include_path() . PATH_SEPARATOR . '/absolute/path');

You can then do all your includes relative to '/absolute/path/'.

Saturday, May 29, 2021
 
cegfault
answered 7 Months ago
17

If you need to determine the base path of a set of scripts, you should not rely on the "current working directory." This can change from executing environment to executing environment.

Instead, base it on a known path.

/includes/class_bootstrap.php knows that it's going to be one directory down from where the base path is going to be, so it can do this:

define('CWD', realpath(dirname(__FILE__) . '/../') );

dirname gets the directory name given in the passed string. If __FILE__ returns C:/wamp/www/vb4/plugins/includes/class_bootstrap.php, then dirname will return C:/wamp/www/vb4/plugins/includes. We then append /../ to it and then call realpath, which turns that relative .. into a real directory: C:/wamp/www/vb4/plugins

Phew.

From that point forward, CWD will operate as you expect. You can require_once CWD . '/includes/init.php' and it will correctly resolve to C:/wamp/www/vb4/plugins/includes/init.php

Also, this may sound stupid but "vb4" may be referring to vBulletin 4, in which case your plugin may already have access to the configuration information that it exposes, including handy things like paths. This may make this entire exercise unnecessary. I intentionally know nothing about vB, otherwise I would point you at their dev docs.

Wednesday, March 31, 2021
 
Exoon
answered 9 Months ago
68

Cookies are simple text headers sent along with the request. CURL allows you to specify those directly using CURLOPT_COOKIE.

curl_setopt($ch, CURLOPT_COOKIE, 'key=value;anotherkey=anothervalue');

If you know what information to send, you can construct your own cookie header this way. The COOKIEJAR/COOKIEFILE options just automate parsing, saving and sending. You'll have to do that manually (read received Cookie headers, create Cookie headers to be send), if you don't want to write to a file.

Wednesday, August 11, 2021
 
u.gen
answered 4 Months ago
27

The includes are evaluated from the location of the running script. When you include another file, you are essentially pulling the contents of that file into the running script at that place.

For files that should evaluate includes relative to the included file's location, you can do this:

/foo/baz.php

include(dirname(__FILE__) . '/bar.inc.php';
include(dirname(__FILE__) . '/../asdf/qwerty.inc.php'

From the documentation:

__FILE__ is The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.

dirname Given a string containing the path of a file or directory, this function will return the parent directory's path.

[http://php.net/manual/en/function.dirname.php] [http://php.net/manual/en/language.constants.predefined.php]

Wednesday, August 25, 2021
 
Steve
answered 4 Months ago
42

You could try asking for a shared lock using the three argument version of tryLock.

Here is the appropriate javadoc: http://download.oracle.com/javase/1.4.2/docs/api/java/nio/channels/FileChannel.html#tryLock%28long,%20long,%20boolean%29

Basically instead of doing lock=channel.tryLock() you would do lock = channel.trylock(0, Long.MAX_VALUE, true)

As an aside, you should be careful with file locking in java. While you can guarantee the locks behave as expected within the JVM you can't necessarily be sure that they will behave as expected accross multiple processes.

Tuesday, November 2, 2021
 
Dale Zak
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share