Read a file that requires another file

No.

Tried to tell you that.

Maybe I’m just over-simplifying the problem, but I fail to see the problem with using get_included_files() given the examples files shown above.

That function will return a list of the files that have been included, as well as the main script being executed. For example, if file.php is that main script then look at offset 0 of the array.

If there were several different includes in each of the files, then sure things might be trickier but there has been no suggestion of that thus far.

Read carefully. get_included_files() gets a list of the includED files and the script that get_included_files() is being called from ONLY. The poster needs to get the incluDING file ONLY.

He doesn’t need to know every file that index.php has included, he needs the included files to know what file called them (index.php for example). get_included_files() DOES NOT do this and has no way to do it no matter how to work it.

If I call get_included_files from functions.ph, it will return false if functions.php does not include any files. It will not return index.php, the including file:

index.php example:

<?php
 /* I am the includING file. */
 include(functions.php);
 echo foobar();
?>

functions.php example:

<?php
 /* This is the includED file */
 /*
  Calling get_included_files() here will return:
 
  Array ([0] => /path/to/httpdocs/functions.php)
 
  Where-as he needs it to show:
 
  Array ([0] => /path/to/httpdocs/index.php)
 */
      function foobar() {
             return 'Foobar is funny.';
      }
?>

Please see: http://www.php.net/manual/en/function.get-included-files.php#69248

LinuxFreelancer, that is incorrect. Please run your code! Calling get_included_files() within functions.php will return an array containing both of the index.php file and functions.php.

The user comment that you linked to is only concerned with including remote files which, again, there has been no previous mention of in this thread.

Salathe is correct. It’s also important to note that get_included_files is location-in-script-dependant… See the first and last entries below…

io1.php


<?php
print_r(get_included_files());
include_once('io2.php');
print_r(get_included_files());
?>

io2.php


<?php
print_r(get_included_files());
?>

Result

Array ( [0] => C:\\xampp\\htdocs\\io1.php ) 
Array ( [0] => C:\\xampp\\htdocs\\io1.php [1] => C:\\xampp\\htdocs\\io2.php ) 
Array ( [0] => C:\\xampp\\htdocs\\io1.php [1] => C:\\xampp\\htdocs\\io2.php ) 

Except that does not do what he needs. He needs the including script name in a variable. To do that we need:

index.php:

 
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
ob_start();
include_once('functions.php');
$contents = ob_get_contents();
ob_end_clean();
echo $contents
?> 

functions.php:

 
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
print_r(get_included_files());
?>

Though this returns the printed array as a string.

After playing with it, I took your solution and modified it and here is a working solution for the poster:

php function:


 
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
 
function getIncluder() {
     $parentPath = get_included_files();
     $parentPath = $parentPath[0];
     $parentPath = explode('/',$parentPath);
     return end($parentPath);
}
 
echo getIncluder();
exit;
?>
 

index.php:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
include_once('functions.php');
?> 

Demo: [B]http://crackfeed.com/inctest/index.php[/B]

I am humbled, I should have worked with your idea instead of arguing. Together we found the solution in full.

Partially yes, it needed to be in a variable though. Printing the result serves no purpose.

Also, why the function was called 3 times in his script is above me. That was not correct.

So the actual solution comes down to this function called within the script you want to question. However, it will be defined in every script being included, so you need to use function_exists() as well.

If you call getIncluder(true) you will get the full path. If false or empty you get the filename only.


 
if (!function_exists('getIncluder')) {
  function getIncluder($showpath=0) {
     $parentPath = get_included_files();
     $parentPath = $parentPath[0];
     if (!$showpath) {
         $parentPath = explode('/',$parentPath);
         return end($parentPath);
     } else {
         return $parentPath;
     }
  }
}
 
echo getIncluder(true); // root path and filename
echo '<br /><br />';
echo getIncluder(); // just filename
 

Demo: http://crackfeed.com/inctest/index.php

Well first, that was my script, not his. My script called the function three times to show the pre, during, and post include differences.

So the actual solution comes down to this function called within the script you want to question. However, it will be defined in every script being included, so you need to use function_exists() as well.

If you call getIncluder(true) you will get the full path. If false or empty you get the filename only.


 
if (!function_exists('getIncluder')) {
  function getIncluder($showpath=0) {
     $parentPath = get_included_files();
     $parentPath = $parentPath[0];
     if (!$showpath) {
         $parentPath = explode('/',$parentPath);
         return end($parentPath);
     } else {
         return $parentPath;
     }
  }
}
 
echo getIncluder(true); // root path and filename
echo '<br /><br />';
echo getIncluder(); // just filename
 

Demo: http://crackfeed.com/inctest/index.php

or you could just do


$basepage = array_shift(get_included_files());

for the full and


$basepage = array_pop(explode('/',array_shift(get_included_files())));

for the filename
and avoid all the is-function-defined-if-not-define-function stuff but yeah.

Why? I found that rather elegant :slight_smile:
Just to make sure multiple files don’t all define that same function, which of course won’t work.

It’s just too bad this doesn’t work in PHP:


function_exists('test') || function test() { return 'a'; }

I mean, this does


defined('TEST') || define('TEST', true);

(I love abusing the lazyness of the OR operator like that :D)

LinuxFreelancer, you seemed oddly defensive. I apologise if any of my posts came across as criticism, that was certainly not the intention at all. As for arriving at some solution, lets hope jjshell is keeping an eye on this thread. (:

For what it’s worth, here are a couple of suggestions on the latest round of code snippets. Both of you could have used the basename() or [url=http://php.net/pathinfo]pathinfo() functions to get the file name from the path – those functions were written specifically for getting the basename/information from file paths. Also, does jjshell want just the file name, the first posts says he wants to read the file contents so the path should be enough?

Secondly, to StarLion, your use of array_shift() would result in a Strict Standards message because it is used on a function return value rather than a variable containing an array. A minor point and one most people would be happy to live with. You could instead use current() in its place, which will return the first item in the array without complaint.

Salathe, nah I just come off that way sometimes sorry, I am working on that actually. lol Good coder bad socializer. Too many hours coding not enough taking my wife out, according to her. lol

StarLion - Keep it simple. I have not benchmarked but I would guess there is nearly zero difference in performance. Also, I believe in, if it works leave it be. It works and is fast. Good 'nuf for me. Well… and mine is customizable.

If the OP had asked for customization, sure.
If the OP had asked for something that needed to be called more than once in the same script, sure.

The OP needed neither, which is why I used native functions rather than put a single-use function in the function table.

Personally, I think mine was simpler. But thats a matter of opinion.

Guy, this board is for helping people and not for having pi**ing contests. Now get over it, he has two possibilities to choose from, mission accomplished; now move on.

I agree with what’re you saying, but you’re scaring me a bit :shifty:

Next time dial it down a notch okay?

Ok just find it annoying when people have to debate symantics and what not.

If it works and does not hinder performance, let it be is what I say.