473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

require_once, undefined function?

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
6 5305
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
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: the wonderer | last post by:
This is an elementary question, but I've not been able to find the answer, so here goes: I am developing a site using php. I have the html header information in a file that I include in all the...
2
by: will taubin | last post by:
i would like my program to have a config.php with passwords and other stuff. i would like to have a functions.php to hold all my functions. i would like the functions.php to include/require...
1
by: Glenn | last post by:
Hi I have been hoping to use my laptop as a porta-server but ran into this problem. It seems that require_once() is not working for me! The child class file has a 'require_once()' to the parent...
11
by: Kimmo Laine | last post by:
I'm flipping my wig here, people. I'm using classes and making each class a file. when I'm including dependet classess, I use require_once to avoid multiple declarations - yet they happen. I put...
8
by: David T. Ashley | last post by:
Hi, Does require_once() treat "file.inc" and "subdirectory/file.inc" as the same things or different? The reason for my question is that I'd like to organize my PHP library into...
6
by: Shelly | last post by:
Here is a crazy question that has happend to me once before. I have an include file for the connection information to the server. It is like this: $hostname= "the_server_location"; $database...
6
by: Royan | last post by:
Ok the problem is quite hard to explain, but i'll try to keep it as simple as i can. Imagine I have the following structure of my files and folders: /root/global.inc |__/files/foo.php...
7
by: Ronald Raygun | last post by:
I have been struggling with this all afternoon and I'm, well lats just say, very pissed off ... I have a require once in a file. I am passing the fully qualified (i.e. absolute pathname) to a...
4
by: Andrew G. Koptyaev | last post by:
Is I can use include_once() or require_once() in function or only include()?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.