Reputation: 143
i have some files with code php and html:
the file (server-root)/folder/folder/test.php :
<?php
$im = file_get_contents("im.txt");
?>
the file (server-root)/folder/folder/im.txt :
88-88-88
i try on the file index.php on the server root:
<?php
include "folder/folder/test.php";
?>
when i include the variable of the other will lost because im.txt is not on server-root but on /folder/folder/im.txt
i also try this file index.php on the server root:
<?php
echo file_get_contents("folder/folder/test.txt");
?>
but is not working.
Am thinking that will be a good idea to execute the file before its included on file, so no php will be on index.php and only show the data of the other file, of the im.txt
so what i have to do?
Upvotes: 0
Views: 792
Reputation: 27884
You can workaround relative path issues by specifying full pathes based on current script folder.
For instance, in folder/folder/test.php
you can do the following:
<?php
echo file_get_contents(__DIR__ . "/im.txt");
?>
It should have the same effect as file_get_contents("im.txt")
when calling test.php
directly, but this time it will work no matter from what relative path you call it.
Upvotes: 2
Reputation: 534
For evaluating/executing php scripts inline within a php script, you could use the eval() function. Here's some more detailed explanation: http://php.net/manual/en/function.eval.php
Upvotes: 0
Reputation: 43755
Ok, I am adding another answer to help you with your revision. I'd like to note that your question is explain backwards. You should start with the main file (index.php). I will explain it this way for more clarity.
Your index.php file at the server root has this:
<?php
include "folder/folder/test.php";
?>
So, we can go ahead and replace the include with the contents of "test.php" and then work from there. That means index.php now looks like this:
<?php
$im = file_get_contents("im.txt");
?>
I feel almost certain that this is where you are lost. The file at your server root is now running that above code. Witch means it is looking for (server-root)/im.txt. But you have said that it is found at (server-root)/folder/folder/im.txt. So, change that line in "test.php" to that directory, like this: $im = file_get_contents("folder/folder/im.txt");
Now, index.php will pull in test.php's code, then look for "im.txt" in the appropriate folder at which point, index.php will look like this:
<?php
$im = '88-88-88';
?>
As I said, there are many ways to deal with the relative links to make them more dynamic, in case you want to include a file in different directories and change what it is relative to. You can even make it relative to "test.php" if you want to. Research php's magic constant DIR , FILE and function called getcwd()
, Those should help you do more advanced things. For now, you could just fix it by adjusting the directory manually.
Further reading:
http://yagudaev.com/posts/resolving-php-relative-path-problem/
Get the current script file name
Upvotes: 1
Reputation: 9920
The main thing that you need to understand is that when including a file into another, relative paths are only relative to the main file, and not relative to each included file.
The solution is quite simple - define
// /var/www/test.php
define("UPLOAD_PATH", "lib/folder/");
include "lib/test.php";
and
// /var/www/lib/test.php
$filePath = UPLOAD_PATH . 'file.txt';
file_get_contents($filePath);
This should be enough to do what you need.
Upvotes: 1
Reputation: 43755
From what I can understand about your situation, you are wondering how an included file will treat variables, such as if directories will be linked from the included file's directory or from the parent file's directory. When including a file, the code that is within that file will be written into the parent file, just as though it were there to begin with. For example:
Parent file:
<?php
$a = 1;
include "child.php";
echo $a+$b; //will display "3"
?>
and child.php:
<?php
$b = 2;
?>
So, if the main file is at the root of your site (example.com/index.php) And index.php has this:
<?php
$a = 1;
include "sub-dir/child.php";
?>
and child.php has this:
<?php
$b = 2;
include "other-child.php";
?>
other-child.php should be in the root directory, NOT in "sub-dir".
Upvotes: 1