[PHP] Is a file included?

Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    :joy: Jonteh is offline
    MemberRank
    Apr 2007 Join Date
    New York, USALocation
    3,375Posts

    [PHP] Is a file included?

    I'm not that new to PHP but i'm still learning how to do things OOP, so while I was re writing my website to be OOP today I came across a need for a function to check if a file was included or not.

    I then wrote the function in my z.core.php file and then forgot why I needed to check, so here it is, if you want it.

    PHP Code:
        public function is_included($filename)
        {
            
    $get get_included_files();
            
            foreach(
    $get as $file)
            {
                
    $included realpath($filename);
                if(
    $file == $included)
                {
                    return 
    true;
                }
                else
                {
                    return 
    false;
                }
            }
        } 
    & here is an example on how to use it:

    PHP Code:
    if(is_included("file.php")){
    echo 
    "file is included";
    }
    else{
    die(
    "not included, soz boz");

    There you go :-)

    Jcat


  2. #2
    Banned c0mma is offline
    BannedRank
    Jan 2011 Join Date
    ^RaGEZONE^Location
    696Posts

    Re: [PHP] Is a file included?

    Sorry, but what is this for? I don't completely understand, but thanks for contributing to RaGEZONE!

  3. #3
    :joy: Jonteh is offline
    MemberRank
    Apr 2007 Join Date
    New York, USALocation
    3,375Posts

    Re: [PHP] Is a file included?

    It's to check if a file is included for debugging purposes, eg something isn't working so you run that to see if the file is included. Sometimes when you include a file and include a file through that file it doesn't work :/

  4. #4
    Live Ocottish Sverlord Joopie is online now
    LegendRank
    Jun 2010 Join Date
    The NetherlandsLocation
    2,773Posts

    Re: [PHP] Is a file included?


  5. #5
    :joy: Jonteh is offline
    MemberRank
    Apr 2007 Join Date
    New York, USALocation
    3,375Posts

    Re: [PHP] Is a file included?

    This doesn't include the file, it just checks if it is included

  6. #6
    Alpha Member Justei is offline
    MemberRank
    Oct 2007 Join Date
    /f241Location
    1,904Posts

    Re: [PHP] Is a file included?

    Looks interesting, might be useful for some people :).

  7. #7
    Infraction Baɴɴed holthelper is offline
    MemberRank
    Apr 2008 Join Date
    1,765Posts

    Re: [PHP] Is a file included?

    sorry but your method looks flawed.

    PHP Code:
    <?php
    public function is_included($filename) {
        
    $get get_included_files();
        if(
    in_array(realpath($filename), $get)) {
            return 
    true;
        } else {
            return 
    false;
        }
    }
    ?>
    extra code to break up the array is not need when you can do a simple search inside the array and return true

  8. #8
    :joy: Jonteh is offline
    MemberRank
    Apr 2007 Join Date
    New York, USALocation
    3,375Posts

    Re: [PHP] Is a file included?

    Quote Originally Posted by holthelper View Post
    sorry but your method looks flawed.

    PHP Code:
    <?php
    public function is_included($filename) {
        
    $get get_included_files();
        if(
    in_array(realpath($filename), $get)) {
            return 
    true;
        } else {
            return 
    false;
        }
    }
    ?>
    extra code to break up the array is not need when you can do a simple search inside the array and return true
    Thanks :)

  9. #9
    Account Upgraded | Title Enabled! AngraMainyu is offline
    MemberRank
    May 2011 Join Date
    445Posts

    Re: [PHP] Is a file included?

    I apologize if this is necroposting, but I'd hate to see someone actually use this code. If you want to do OOP properly -- without the overhead of verifying every file before use in every script each time it's requested:

    PHP Code:
    <?php

        set_error_handler
    (function($errno$errstr$errfile$errline$errcontext) {
            throw new 
    Exception("Exception caught in " $errfile " on line " $errline ": " $errstr$errno);
            return 
    true;
        });
        
        try {
            include_once(
    "file.php");
        }
    ?>
    or if you need your application to be really fast:

    PHP Code:
    <?php
        
    require_once("file.php");
    ?>

  10. #10
    Member sagenessamerda is offline
    MemberRank
    Jul 2011 Join Date
    66Posts

    Re: [PHP] Is a file included?

    PHP Code:
    <?php
    public function is_included($filename) {
        return 
    in_array(realpath($filename), get_included_files());
    }
    ?>

  11. #11
    Ginger by design. jMerliN is offline
    MemberRank
    Feb 2007 Join Date
    2,497Posts

    Re: [PHP] Is a file included?

    O(n) solutions suck.

    Suck hard, too.

  12. #12
    Infraction Baɴɴed holthelper is offline
    MemberRank
    Apr 2008 Join Date
    1,765Posts

    Re: [PHP] Is a file included?

    Quote Originally Posted by sagenessamerda View Post
    PHP Code:
    <?php
    public function is_included($filename) {
        return 
    in_array(realpath($filename), get_included_files());
    }
    ?>
    HA could have done that but wasnt thinking too much about it. if i took a sec more to look at it i would have came up with your function.

  13. #13
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,098Posts

    Re: [PHP] Is a file included?

    Quote Originally Posted by jMerliN View Post
    O(n) solutions suck.

    Suck hard, too.
    Because it results in code like this anyway?
    PHP Code:
    // way we did it without ur function
    $f realpath('some_file.php');

    if( 
    in_array($fget_included_files()) )
    {
        
    printf"File, '%s' is included."$f );
    } else {
        include( 
    $f );
    }

    // way we do it with ur function
    function is_included($filename

        return 
    in_arrayrealpath($filename), get_included_files() ); 
    }

    $f 'some_file.php';

    if( 
    is_included($f) )
    {
        
    printf"File, '%s' is included."$f );
    } else {
        include( 
    $f );

    When actually it could be avoided entirely using this,
    PHP Code:
    require_once( 'some_file.php' ); 
    which also resembles O(n).. Shit, almost everything in PHP does.

    I think in PHP that's accepted by now.
    Last edited by s-p-n; 20-10-11 at 06:49 PM.

  14. #14
    Ginger by design. jMerliN is offline
    MemberRank
    Feb 2007 Join Date
    2,497Posts

    Re: [PHP] Is a file included?

    Quote Originally Posted by s-p-n View Post
    Because it results in code like this anyway?
    PHP Code:
    // way we did it without ur function
    $f realpath('some_file.php');

    if( 
    in_array($fget_included_files()) )
    {
        
    printf"File, '%s' is included."$f );
    } else {
        include( 
    $f );
    }

    // way we do it with ur function
    function is_included($filename

        return 
    in_arrayrealpath($filename), get_included_files() ); 
    }

    $f 'some_file.php';

    if( 
    is_included($f) )
    {
        
    printf"File, '%s' is included."$f );
    } else {
        include( 
    $f );

    When actually it could be avoided entirely using this,
    PHP Code:
    require_once( 'some_file.php' ); 
    which also resembles O(n).. Shit, almost everything in PHP does.

    I think in PHP that's accepted by now.
    I doubt that's O(n). Using a simple hash table you can achieve O(1) expected time.

  15. #15
    Account Upgraded | Title Enabled! AngraMainyu is offline
    MemberRank
    May 2011 Join Date
    445Posts

    Re: [PHP] Is a file included?

    Quote Originally Posted by jMerliN View Post
    I doubt that's O(n). Using a simple hash table you can achieve O(1) expected time.
    In PHP, associative arrays are implemented as hash tables. Any operation that does not involve iteration is O(1).



Page 1 of 2 12 LastLast

Advertisement