SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 35
  1. #1
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    read a file that requires another file

    Hello,

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

    file.php
    PHP Code:
    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.

  2. #2
    SitePoint Zealot ChrisWiegman's Avatar
    Join Date
    Sep 2010
    Location
    Austin, Texas, United States
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Zealot LinuxFreelancer's Avatar
    Join Date
    Jun 2011
    Location
    Boston, Ma, Usa
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try something like....

    PHP Code:
       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.

  4. #4
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much.

    How would you automatically detect $filename?

  5. #5
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Zealot LinuxFreelancer's Avatar
    Join Date
    Jun 2011
    Location
    Boston, Ma, Usa
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are a few ways, I prefer putting a variable in your main config file named $path_to:

    PHP Code:
    $path_to 'http://domain.com/'
    and then just do this:

    PHP Code:
    require_once($path_to 'system/script.php'); 
    That works perfectly on any server, and OS and any version of PHP.

  7. #7
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  8. #8
    SitePoint Zealot LinuxFreelancer's Avatar
    Join Date
    Jun 2011
    Location
    Boston, Ma, Usa
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jjshell View Post
    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
    The simplest way is:

    PHP Code:
    require_once('../index.php'); // one directory down
    require_once('../../index.php'); // two directories down 
    then there is:

    PHP Code:
    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

  9. #9
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  10. #10
    SitePoint Zealot LinuxFreelancer's Avatar
    Join Date
    Jun 2011
    Location
    Boston, Ma, Usa
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?php
    $includingScript 
    $_SERVER['PHP_SELF'];
    // Now functions.pp will know the name of the including script.
    require('functions.php');
    ?>

  11. #11
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You could, possibly, use debug_backtrace for this; however I'd guess that there is a simpler was to achieve what you're trying to do.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  12. #12
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @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.


  13. #13
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    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 DEFINE'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.

  14. #14
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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

  15. #15
    SitePoint Zealot LinuxFreelancer's Avatar
    Join Date
    Jun 2011
    Location
    Boston, Ma, Usa
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If this is to prevent hotlinking, opening say /system.functions.php in a browser, as example, then just put in the main files:

    PHP Code:
    <?php
    $safeVar 
    true;
    ?>
    and in the files that should not be accessible in browsers:

    PHP Code:
    <?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.

  16. #16
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jjshell View Post
    I couldn't find one that returns the name of the including/requiering file.
    get_included_files() can tell you that.
    Salathe
    Software Developer and PHP Manual Author.

  17. #17
    SitePoint Zealot LinuxFreelancer's Avatar
    Join Date
    Jun 2011
    Location
    Boston, Ma, Usa
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    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.

  18. #18
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    LinuxFreelancer, given the file setup described in the thread and how that function works, that's exactly what he can do.
    Salathe
    Software Developer and PHP Manual Author.

  19. #19
    SitePoint Zealot LinuxFreelancer's Avatar
    Join Date
    Jun 2011
    Location
    Boston, Ma, Usa
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How is get_included_files() 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.

  20. #20
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    file.php
    PHP Code:
    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?

  21. #21
    SitePoint Zealot LinuxFreelancer's Avatar
    Join Date
    Jun 2011
    Location
    Boston, Ma, Usa
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No.

    Tried to tell you that.

  22. #22
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    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.
    Salathe
    Software Developer and PHP Manual Author.

  23. #23
    SitePoint Zealot LinuxFreelancer's Avatar
    Join Date
    Jun 2011
    Location
    Boston, Ma, Usa
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?php
     
    /* I am the includING file. */
     
    include(functions.php);
     echo 
    foobar();
    ?>
    functions.php example:
    PHP Code:
    <?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/functio...iles.php#69248

  24. #24
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    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
    Software Developer and PHP Manual Author.

  25. #25
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?php
    print_r
    (get_included_files());
    include_once(
    'io2.php');
    print_r(get_included_files());
    ?>
    io2.php
    PHP Code:
    <?php
    print_r
    (get_included_files());
    ?>
    Result
    Code:
    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 )


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •