this works for me, but is there a better/safer way to use an include:
<?php include "http://www.myserver.com/somefile.php" ; ?>
I tried:
<?php include "somefile.php" ; ?>
but it did not work.
this works for me, but is there a better/safer way to use an include:
<?php include "http://www.myserver.com/somefile.php" ; ?>
I tried:
<?php include "somefile.php" ; ?>
but it did not work.
I use this format so I can use relative paths from the root level. It makes it easier to read than a bunch of ..\..\ in a path.
include $_SERVER["DOCUMENT_ROOT"] . '/root folder/file.php'
There are several things wrong here:
readfile
or file_get_contents
.The reason your second include doesn't work is because the file somefile.php
is not in the "working directory" (likely the directory of the file in where you have the include).
Do one of these:
/path/to/somefile.php
(on Unix-like systems).../relative/path/to/somefile.php
.For the best portability & maintence, I would have some kind of constant defined to the base path, and use that when looking for files. This way if something in the file system changes you only have to change one variable to keep everything working.
example:
define('INCLUDE_DIR','/www/whatever/');
include(INCLUDE_DIR . 'somefile.php');
I would highly suggest setting an include_path and then using PHP's autoload:
My autoloader looks like this:
function __autoload( $class_name ) {
$fullclasspath="";
// get separated directories
$pathchunks=explode("_",$class_name);
//re-build path without last item
$count = count( $pathchunks ) -1;
for($i=0;$i<$count;$i++) {
$fullclasspath.=$pathchunks[$i].'/';
}
$last = count( $pathchunks ) - 1;
$path_to_file = $fullclasspath.$pathchunks[$last].'.php';
if ( file_exists( $path_to_file ) ) {
require_once $path_to_file;
}
}
I use this :
$filename = "file.php";
if (file_exists($filename)) {
include($filename);
}