Page 1 of 1

require_once() on a class.php file

Posted: Wed Oct 17, 2012 1:42 am
by btowle
Hi,

This may be a stupid question but it has me and my team stumped. We need to back up the execution results as file as we make them. That means when we run the execution code it creates an xml file, if we add a bug it modifies the xml file, and if we add an attachment it will also modify the xml file.

I have all the code working for creating the file and modifying it. However, when I login I get the following error:
Fatal error: require_once(): Failed opening required '../../config.inc.php' (include_path='.:/usr/share/php:/usr/share/pear:.:/var/www/testlink/lib/functions/:/var/www/testlink/lib/issuetrackerintegration/:/var/www/testlink/third_party/') in /var/www/testlink/lib/functions/tlAttachmentRepository.class.php on line 24
But get this, I am actually logged in, if I change the url I can access the testlink server fine.
Now in the tlAttachmentRepository.class.php I added these two lines:

require_once('../../config.inc.php');
require_once('../execute/exec_duplicate.lib.php');

I include the config.inc.php to determine if I have execution_duplication enabled, and the other file is my own library for backing up the result files.
If I comment both of them out I can login normal. (Obviously the modified code won't work)

I am sure this is a stupid error on my part, but I can't figure out why including these two files would actually cause a problem.

Any help would be greatly appreciated,

Thanks,
BTowle

Re: require_once() on a class.php file

Posted: Wed Oct 17, 2012 6:30 am
by GunnarD
Your error message say "I cant find ../../config.inc.php"

The problem with relativ paths (ex. ../../config.inc.php) is that you must know in witch directory your script is running in.

Try to put this code just before require_once to see what the current working directory is:

Code: Select all

echo getcwd() . "\n";

Re: require_once() on a class.php file

Posted: Wed Oct 17, 2012 3:54 pm
by btowle
So when I go to the login.php and I get the following:
/var/www/testlink

Which makes sense, that's where the login.php is. So why is it trying to invoke tlAttachmentRepository.php I didn't change anything in the login.php?
Futhermore, I put the echo getcwd() inside the tlAttachmentRepository.php and it is not in the testlink root directory.

Thanks for your feedback, I am sure it is something silly that I don't know with php. Does testlink go through (on startup) and load all the .class.php files?

Thanks again,
Btowle

Re: require_once() on a class.php file

Posted: Wed Oct 17, 2012 4:05 pm
by btowle
Just a quick update whatever is happening is occurring in the login.php
I put some echo statements before the redirect commands and it is not reaching that point in code.

Re: require_once() on a class.php file

Posted: Wed Oct 17, 2012 4:44 pm
by btowle
So I was able to trace it to the problem:
In Login.php it calls doAuthorize

In doAuthorize it calls

setUserSession
and then dies when it creates a testproject object.
Inside this constructor the tlObjectWith Attachments class is created (I believe statically)
and inisde the tlObjectWithAttachments constructor the attachmentRepository is created (again statically).


Is it be cause these files are being created without the new command, I don't understand why having a relative file path would not work in the tlAttachmentRepository.class.php?
I will try testlink root.

But I am curious why this is breaking, just for my own knowledge.

Cheers,
Btowle

Re: require_once() on a class.php file

Posted: Wed Oct 17, 2012 5:04 pm
by btowle
If I do imports from the testlink root directory
aka:
require_once("config.inc.php");
require_once("lib/functions/common.php");


It will still fail... :?

Re: require_once() on a class.php file

Posted: Wed Oct 17, 2012 5:29 pm
by btowle
So this was my solution:

Code: Select all

$check = getcwd();
if(strpos("/testlink/lib/",$check))
{
	require_once('../../config.inc.php');
	require_once('../execute/exec_duplicate.lib.php');
}
else if(strpos("/testlink/",$check))
{
  	require_once('config.inc.php');
	require_once('./lib/execute/exec_duplicate.lib.php');
}
The top check is after testlink is up and running,
the bottom check occurs when you first log into testlink.

Is there a better solution?

Re: require_once() on a class.php file

Posted: Wed Oct 17, 2012 5:49 pm
by fman
my suggestion is:
copy what already have been done on other files that live in SAME directory where you want to put your development.

Please remember that there is a set to instruct PHP where to look for files when doing require.
Give a look to
ini_set('include_path', ...

present on cfg/const.inc.php
this may be can help.