[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
Re: [PHP] Is a file included?
Sorry, but what is this for? I don't completely understand, but thanks for contributing to RaGEZONE!
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 :/
Re: [PHP] Is a file included?
Re: [PHP] Is a file included?
This doesn't include the file, it just checks if it is included
Re: [PHP] Is a file included?
Looks interesting, might be useful for some people :).
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
Re: [PHP] Is a file included?
Quote:
Originally Posted by
holthelper
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 :)
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");
?>
Re: [PHP] Is a file included?
PHP Code:
<?php
public function is_included($filename) {
return in_array(realpath($filename), get_included_files());
}
?>
Re: [PHP] Is a file included?
O(n) solutions suck.
Suck hard, too.
Re: [PHP] Is a file included?
Quote:
Originally Posted by
sagenessamerda
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.
Re: [PHP] Is a file included?
Quote:
Originally Posted by
jMerliN
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($f, get_included_files()) )
{
printf( "File, '%s' is included.", $f );
} else {
include( $f );
}
// way we do it with ur function
function is_included($filename)
{
return in_array( realpath($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.
Re: [PHP] Is a file included?
Quote:
Originally Posted by
s-p-n
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($f, get_included_files()) )
{
printf( "File, '%s' is included.", $f );
} else {
include( $f );
}
// way we do it with ur function
function is_included($filename)
{
return in_array( realpath($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.
Re: [PHP] Is a file included?
Quote:
Originally Posted by
jMerliN
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).