SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 35 of 35
  1. #26
    SitePoint Zealot LinuxFreelancer's Avatar
    Join Date
    Jun 2011
    Location
    Boston, Ma, Usa
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?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 Code:
    <?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 Code:
     
    <?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 Code:
    <?php
    ini_set
    ('display_errors'1);
    error_reporting(E_ALL);
    include_once(
    'functions.php');
    ?>
    Demo: http://crackfeed.com/inctest/index.php

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

  2. #27
    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 StarLion View Post
    Salathe is correct.
    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.


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

  3. #28
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by LinuxFreelancer View Post
    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.
    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.


    PHP Code:
     
    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
    PHP Code:
    $basepage array_shift(get_included_files()); 
    for the full and
    PHP Code:
    $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.

  4. #29
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,095
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by StarLion View Post
    and avoid all the is-function-defined-if-not-define-function stuff
    Why? I found that rather elegant
    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:

    PHP Code:
    function_exists('test') || function test() { return 'a'; } 
    I mean, this does

    PHP Code:
    defined('TEST') || define('TEST'true); 
    (I love abusing the lazyness of the OR operator like that )
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

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

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

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

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

  9. #34
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,095
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by LinuxFreelancer View Post
    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

    Next time dial it down a notch okay?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

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


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
  •