@JameB, It's rarely a good idea to include files using the HTTP protocol, and it's even rarer that you'd want to include a text file using the include command. Typically you'd want to read such files in using file_get_contents or a fopen handler.
I can only echo if I have a file in the same folder. Do I need to set something global?
No, the location of the included PHP file shouldn't matter at all in that regard. It's more likely that something is going wrong in the included code because of the changed path.
Keep in mind that when you incldue a PHP file into another PHP file, the currrent working directory (CWD) doesn't change. That is, the CWD will remain on the path to the file that does the including, even for the code inside the included file.
So, say that you have three files, structured like this:
/var/www/ (web-root)
index.php
apps/
page1/
app.php
data.txt
You could write the two page1 files like this without having a problem:
// apps/page1/data.txt
first: Value for the first var.
second: And the value for the second var.
third: Etc...
And:
<?php
// apps/page1/app.php
$file_path = "data.txt";
$contents = file_get_contents($file_path);
$lines = preg_split("/\n\r?/", $contents);
foreach ($lines as $line) {
$parts = explode(":", $line);
if (count($parts) == 2 && !empty($parts[0]) && !empty($parts[1])) {
${"name_" . ($parts[0])} = trim($parts[1]);
}
}
If you call the apps.php file by itself, there is no problem. The code generates the name_first, name_second, and name_third variables fine, and you could use them in that file as you see fit.
However, if you included the app.php into the index.php file two directories lower in the hierarchy, then you've got a problem. The CWD of the index script would be /var/www, so when you call file_get_contents("data.txt") in the app.php file, then you are actually trying to read: /var/www/data.txt, as opposed to the file in the same directory as that script.
You need to be careful about these types of things when dealing with included files. One way is to use the __DIR__ constant, which is always set to the directory of the script it's in.
$file_path = __DIR__ . "/data.txt";
This way you can define the path relative to the app.php file, no matter where it's included.
Note that the above will only work in PHP 5.3 and higher. In older versions, you can get the same results with the __FILE__ constant and the dirname function
$file_path = dirname(__FILE__) . "/data.txt"