Read a file that requires another file

Hello,

Sory if the title isn’t super clear. But check this out:

file.php


Hello world.
<?php require_once('readfile.php'); ?>

And readfile.php would open file.php and get its content into a string.

Is it something feasible? If yes, what would be the most elegant way (I really don’t know where to look/start).

Regards,

-jj. :slight_smile:

While this would depend on the actions of your code, what your doing is both correct and an acceptable practice. Using “require_once” the server will essentially treat the code in the required file as if it is part of the script on the originating file. Take a look through any of the big CMS apps (Wordpress, Drupal, etc) and you will see a string of includes and requires starting from the root index.php.

Try something like…


   ob_start();
   require_once($filename);
   $contents = ob_get_contents();
   ob_end_clean();
 
   echo $contents;

That buffers the script’s output into a var. Should work fine.

Thank you very much.

How would you automatically detect $filename?

I’m sorry, what I mean is: how do you get the path to the including file. Is there such a function? I know that FILE won’t work in this context.

There are a few ways, I prefer putting a variable in your main config file named $path_to:

 
$path_to = 'http://domain.com/';

and then just do this:

 
require_once($path_to . 'system/script.php');

That works perfectly on any server, and OS and any version of PHP.

Yep, I would have done that.

But I really need the included file to know the path to the file including it without setting a variable.

Thanks :slight_smile:

The simplest way is:


require_once('../index.php'); // one directory down
require_once('../../index.php'); // two directories down

then there is:


require_once(dirname(__FILE__) . '/index.php');
 
// and
 
require_once(dirname(dirname(__FILE__)).'/index.php);
 
// http://php.net/manual/en/function.dirname.php

Also read about:

PHP: basename - Manual

PHP: pathinfo - Manual

PHP: realpath - Manual

Hello,

Thanks again for the reply! I read the links you posted, but I couldn’t find one that returns the name of the including/requiering file.

My goal is to avoid hard-typing anything.

I have never heard of such functionality. Hardcoding is your only option as far as I know. You’d have to create a var that has the script name in it prior to including the other script.

Example:


<?php
$includingScript = $_SERVER['PHP_SELF'];
// Now functions.pp will know the name of the including script.
require('functions.php');
?>

You could, possibly, use [fphp]debug_backtrace[/fphp] for this; however I’d guess that there is a simpler was to achieve what you’re trying to do.

@LF: good idea, but no clue in the including file is acceptable. The included file really has to find by itself who is including it.

@AS: I’ll get a look at that.

:slight_smile:

I may be reading between the lines here, but it sounds as if you’re trying to prevent hotlinking/cross-including the file; this is often done by [FPHP]DEFINE[/FPHP]'ing a constant in your intended includer, and checking for it in the file to be included. (A variable would also accomplish the same thing)

Obviously this becomes less effective if you distribute your code, as it relies upon noone else knowing what constant you’ve defined.

It does not sound the case, but if your included file is coming from a $var that emanates from user input be super aware of Directory Traversal - How to find & fix it

If this is to prevent hotlinking, opening say /system.functions.php in a browser, as example, then just put in the main files:


<?php
$safeVar = true;
?>

and in the files that should not be accessible in browsers:


<?php
 
if (!$safeVar) {
die("GTFO!");
}
 
?>

As far as what you are asking, as far as I know “It cannot be done”. So, find another way to achieve your goal.

get_included_files() can tell you that.

He doesn’t want the included files, he wants the includING files.

So if index.php includes functions.php, then functions.php needs to know which file included it.

LinuxFreelancer, given the file setup described in the thread and how that function works, that’s exactly what he can do.

How is [FONT=monospace][COLOR=#0055bb]get_included_files()[/COLOR][/FONT] going to tell the included script which file included it? Not to mention he wants only the including file, not a list of all included files.

file.php


require_once('included_file.php');

Is there a way for included_file.php to know the path to file.php (the file including it) without hardtyping anything to give hints?