By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,716 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,716 IT Pros & Developers. It's quick & easy.

require_once, undefined function?

P: n/a
Say I have a script:

http://www.mydomain.com/test/admin/foo.php

That looks like this:
<?php
$baseDir = "http://" . $_SERVER[ "SERVER_NAME" ] . "/test";

require_once( $baseDir . '/helpers/helper.php' );

something();
?>

something() is a function in helper.php.

I get:
Call to undefined function: something() in
/home/mydomain/public_html/test/admin/foo.php on line 6

The require_once doesn't fail (well, I don't get an error message), so I'm
assuming that it finds helper.php OK. something() definitely exists in
helper.php.

If I put foo.php and helper.php in the same folder, lose all the $baseDir
crap and use require instead of require_once it works fine (not sure if it's
the $baseDir part or the require_once that is tripping it up), however I
need them to be in seperate folders and I'd rather use require_once than
require in case I end up including something twice by accident, it's so much
easier not to have to worry about it.

I've been fiddling with this for about an hour now and it's starting to get
really annoying.

Any ideas?

--
"Come to think of it, there are already a million monkeys on a million
typewriters, and the Usenet is NOTHING like Shakespeare!" - Blair Houghton
-=-=-=-=-=-=-=-=-=-=-=-
http://www.nrkn.com/
-=-=-=-=-=-=-=-=-=-=-=-
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Nik Coughin <nr***********@woosh.co.nz> wrote:
$baseDir = "http://" . $_SERVER[ "SERVER_NAME" ] . "/test";

require_once( $baseDir . '/helpers/helper.php' );

something();
?>

something() is a function in helper.php.


You are requesting the php file by http, that will probably mean that
the php file in interpreted on that webserver and you are only getting
yhe output of the script, _not_ the script _itself_ (jus tlike making
the request from a webbrowser). If you really want to include remove
script (and IMHO you shouldn'y), make sure you are actually getting php
(either by outputting the unprocessed PHP or generating PHP by the use
of PHP).

--

Daniel Tryba

Jul 17 '05 #2

P: n/a
Daniel Tryba wrote:
Nik Coughin <nr***********@woosh.co.nz> wrote:
$baseDir = "http://" . $_SERVER[ "SERVER_NAME" ] . "/test";

require_once( $baseDir . '/helpers/helper.php' );

something();


something() is a function in helper.php.


You are requesting the php file by http, that will probably mean that
the php file in interpreted on that webserver and you are only getting
yhe output of the script, _not_ the script _itself_ (jus tlike making
the request from a webbrowser). If you really want to include remove
script (and IMHO you shouldn'y), make sure you are actually getting
php (either by outputting the unprocessed PHP or generating PHP by
the use of PHP).


Well, that makes sense. I don't want to include a remote script. I just
want to be able to include files in other files from anywhere in my
directory tree. Maybe I should be putting the folders of files that I want
included in my include path instead.
Jul 17 '05 #3

P: n/a
Nik Coughin <nr***********@woosh.co.nz> wrote:

Well, that makes sense. I don't want to include a remote script. I just
want to be able to include files in other files from anywhere in my
directory tree. Maybe I should be putting the folders of files that I want
included in my include path instead.


Good idea, alternatives are using relative paths (need extra work when
script gets moved) or create an absolute path with eg document_root.

--

Daniel Tryba

Jul 17 '05 #4

P: n/a
Nik Coughin <nr***********@woosh.co.nz> wrote or quoted:
I just want to be able to include files in other files from anywhere in
my directory tree. [...]


I currently use the following for that:

function path_relative_to_here($path) {
$from = realpath(dirname($_SERVER["SCRIPT_FILENAME"]));
$to = realpath(dirname(__FILE__)."/".$path);
return relative_path($to,$from);
}

function relative_path($targetfile, $basedir = '.') {
$basedir = realpath ($basedir);
$targetfile = realpath ($targetfile);

// on windows, check that both paths are on the same drive
if (substr ($basedir, 0, 1) != substr ($targetfile, 0, 1)) {
return false;
}

// split each path into its directories
$base_parts = split ('\/', str_replace ('\\', '/', $basedir));
$target_parts = split ('\/', str_replace ('\\', '/', $targetfile));

// ensure that there are no empty elements at the end (c:\ would cause it)
for ($i = count($base_parts) - 1; $i >= 0; $i--) {
if ($base_parts[$i] == '') {
unset ($base_parts[$i]);
} else {
break;
}
}
for ($i = count($target_parts) - 1; $i >= 0; $i--) {
if ($target_parts[$i] == '') {
unset ($target_parts[$i]);
} else {
break;
}
}

// get rid of the common directories at the beginning of the paths
$common_count = 0;
for ($i = 0; $i < count($base_parts); $i++) {
if ($target_parts[$i] == $base_parts[$i]) {
$common_count++;
} else {
break;
}
}
for ($i = 0; $i < $common_count; $i++) {
unset ($base_parts[$i]);
unset ($target_parts[$i]);
}

// build the resulting string
$cnt = count($base_parts);

return str_repeat ('../', $cnt).implode('/', $target_parts);
}

To use it:

require_once(path_relative_to_here("relative_path_ to_file.inc.php"));

Improvements welcomed.
--
__________
|im |yler http://timtyler.org/ ti*@tt1lock.org Remove lock to reply.
Jul 17 '05 #5

P: n/a
"Nik Coughin" <nr***********@woosh.co.nz> wrote in message news:<yh********************@news.xtra.co.nz>...
Say I have a script:

http://www.mydomain.com/test/admin/foo.php

That looks like this:
<?php
$baseDir = "http://" . $_SERVER[ "SERVER_NAME" ] . "/test";

require_once( $baseDir . '/helpers/helper.php' );

something();
?>

http:// don't use absolute path.you may use related path or document
root environment variables.:-)
something() is a function in helper.php.

I get:
Call to undefined function: something() in
/home/mydomain/public_html/test/admin/foo.php on line 6

The require_once doesn't fail (well, I don't get an error message), so I'm
assuming that it finds helper.php OK. something() definitely exists in
helper.php.

If I put foo.php and helper.php in the same folder, lose all the $baseDir
crap and use require instead of require_once it works fine (not sure if it's
the $baseDir part or the require_once that is tripping it up), however I
need them to be in seperate folders and I'd rather use require_once than
require in case I end up including something twice by accident, it's so much
easier not to have to worry about it.

I've been fiddling with this for about an hour now and it's starting to get
really annoying.

Any ideas?

Jul 17 '05 #6

P: n/a
Tim Tyler wrote:
Nik Coughin <nr***********@woosh.co.nz> wrote or quoted:
I just want to be able to include files in other files from anywhere
in my directory tree. [...]


I currently use the following for that:

8<

Thanks a lot Tim, I'll have a play with it. I've just popped everything
into the same folder for now, but it's a bloody horrible messy way to do
things.
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.