I am trying to include the Zend framework, but I keep getting this error,
Warning: require_once(Zend/Json.php) [function.require-once]: failed to open stream: No such file or directory in /usr/www/users/eyelogicy/zone.eyelogic.co.za/weskom/form/classes/ZendFramework/Zend/Json/Decoder.php on line 25
here is the PHP code I am using to include the framework,
set_include_path('classes/ZendFramework' . PATH_SEPARATOR . get_include_path());
require_once 'Zend/Json/Decoder.php';
Any ideas?
Thanx in advance!
Try an absolute path for the include path.
Using absolute path is also a good practise advice:
One trivial optimization you can do to increase the speed of class loading is to pay careful attention to your include_path. In particular, you should do four things: use absolute paths (or paths relative to absolute paths), reduce the number of include paths you define, have your Zend Framework include_path as early as possible, and only include the current directory path at the end of your include_path.
Related
PHP-programmers!
I've got a problem with the Include-function in PHP.
I have a website which contains a left column-bar, and that column contains dynamic content which I get with the Include-function with a relative path, because absolute paths isn't available in the Include-function.
When I navigate to other files in other folders I get the error: include(folder/fileToBeIncluded.php) [function.include]: failed to open stream: No such file or directory in /home/mywebsite/public_html/thissite/folder/subfolder/leftcolumn.php on line 3
How am I going to deal with that? I am totally lost, and I've been searching for Google and StackOverflow for about 10 minutes now.
Thank you!
Absolute paths are can be used with the include statement.
Try:
include $_SERVER['DOCUMENT_ROOT'] . '/folder/fileToBeIncluded.php';
// or
include dirname(__FILE__) . '/../fileToBeIncluded.php'; // relative to the path of the file doing the include
At the very least, you can hardcode the path to be absolutely sure (until you move your site elsewhere):
include '/home/yoursite/public_html/folder/fileToInclude.php';
Please see include documentation.
May I suggest creating a dedicated "includes" folder? Something like:
/home/mywebsite/includes
in php.ini, you'd need to add:
include_path = "/home/mywebsite/includes/"
That way, all calls to include check from /home/mywebsite/includes for the file you need before looking for it relative to the location of the current page being process.
Here is directory structure
/global.php
/includes/class_bootstrap.php
/includes/init.php
/plugins/myplugin.php
Here is codes in these files
/start.php
require('./includes/class_bootstrap.php');
/includes/class_bootstrap.php
define('CWD', (($getcwd = getcwd()) ? $getcwd : '.'));
require_once(CWD . '/includes/init.php');
/plugins/myplugin.php
require_once(dirname(__FILE__).'../global.php');
And as far as I am understanding the problem is in class_bootstrap.php file coz it's generating wrong path for CWD
here is error:
Warning: require_once(C:/wamp/www/vb4/plugins/includes/init.php) [function.require-once]:
failed to open stream: No such file or directory in C:/wamp/www/vb4/global.php on line 35
As you can see "C:/wamp/www/vb4/plugins/includes/init.php" is wrong path.
The MAIN PROBLEM is that I can edit only myplugin.php file other files are CMS core files and should not be changed.
How can I fix this issue?
If you need to determine the base path of a set of scripts, you should not rely on the "current working directory." This can change from executing environment to executing environment.
Instead, base it on a known path.
/includes/class_bootstrap.php knows that it's going to be one directory down from where the base path is going to be, so it can do this:
define('CWD', realpath(dirname(__FILE__) . '/../') );
dirname gets the directory name given in the passed string. If __FILE__ returns C:/wamp/www/vb4/plugins/includes/class_bootstrap.php, then dirname will return C:/wamp/www/vb4/plugins/includes. We then append /../ to it and then call realpath, which turns that relative .. into a real directory: C:/wamp/www/vb4/plugins
Phew.
From that point forward, CWD will operate as you expect. You can require_once CWD . '/includes/init.php' and it will correctly resolve to C:/wamp/www/vb4/plugins/includes/init.php
Also, this may sound stupid but "vb4" may be referring to vBulletin 4, in which case your plugin may already have access to the configuration information that it exposes, including handy things like paths. This may make this entire exercise unnecessary. I intentionally know nothing about vB, otherwise I would point you at their dev docs.
Not sure what I'm doing wrong here, all I thought I had to down was change my PHP ini settings to
include_path = ".;c:\Program Files (x86)\WAMP\www\Zend\"
But it's not working.
On my script I simply have:
require_once "Date.php";
But get the errors:
Warning: require_once(Zend/Date/DateObject.php) [function.require-once]: failed to open stream: No such file or directory in C:\Program Files (x86)\WAMP\www\Zend\Date.php on line 25
Fatal error: require_once() [function.require]: Failed opening required 'Zend/Date/DateObject.php' (include_path='.;c:\Program Files (x86)\WAMP\www\Zend\') in C:\Program Files (x86)\WAMP\www\Zend\Date.php on line 25
Any insight in to what I am doing wrong is greatly appreciated.
Thanks.
You should not add the Zend Framework directory to the include path. You should add it's parent folder to the include path.
Thus, include_path = ".;c:\Program Files (x86)\WAMP\www\Zend\" would become include_path = ".;c:\Program Files (x86)\WAMP\www\".
After setting up your include path like this, you should use require_once 'Zend/Date.php' instead of require_once 'Date.php'. This is because there are still a lot of require calls inside the framework itself, each pointing to Zend/<classname>.
Your include path is wrong - you can either define it manually or (better) change your php.ini to add the location of your requred includes...
http://www.geeksengine.com/article/php-include-path.html
edit: these may help you
Trouble setting up php Zend include path
http://devzone.zend.com/article/4683
Everybody here suggests you add the library to your include path. I actually disagree with that. Most hosting providers does not have the ZF on a include path, and does not allow you to add it to one. So why set it up like that on your development environment; only to change it on production?
I suggest you create a library folder in your root; put the ZF in there and add that in your APPLICATION to the include path.
E.g. c:\wamp\www\library\Zend
Then for each application add the library in the index.php (you will just go one more folder up):
set_include_path(implode(PATH_SEPARATOR, array(
realpath(dirname(__FILE__) . '/../../library'),
get_include_path(),
)));
This allows you to easily update your ZF library.
It also allows you to easily copy/svn your projects without including the ZF framework.
Most people have their own style. I agree with most that you must include the library directory and not the Zend directory.
Set include path:
include_path = ".;c:\Program Files (x86)\WAMP\www\"
After:
require_once "Zend/Date.php";
You should not add the Zend/ directory to your include path but either the root of your your library folder:
If your Zend library is in www/
your include path must be: c:\Program Files (x86)\WAMP\www\
However, if ZF is in Zend/library/ it should be:
c:\Program Files (x86)\WAMP\www\Zend\library\
It is because how the file are required.
Zend_Date requires Zend/Date/DateObject from Zend/ so you need to include top level directory.
Note, that you can also use the autoloader to do the work for you if you need other Zf classes and don't want to include/require them all.
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
Will allow you to do $date = new Zend_Date(); without require manually any files (except Loader of course!)
I'm having problems assigning a relative path to require_once. I'm sure it's something simple that I'm not seeing...
folder directory structure
level 1: site
level 2: include
level 2: class
so...
site/include/global.php <-- this is where function autoload is being called from
site/class/db.php
when I attempt to use
function__autoload($className)
{
require_once '../class/'.$className.'.php';
}
i get:
Warning: require_once(../class/db.php) [function.require-once]: failed to open stream: No such file or directory
Fatal error: require_once() [function.require]: Failed opening required '../class/db.php' (include_path='.;./includes;./pear')
What am I doing wrong? It'll work fine if I plop the global.php in the class folder so I'm assuming it's my poor understanding of relative paths that is causing the problem..
Thanks
require(_once) and include(_once) work relative to the path of the first script, i.e. the script that was actually called.
If you need to require something in an included or required file, and would like to work relative to that file, use
dirname(__FILE__)."/path/to/file";
or as #Arkh points out, from PHP 5.3 upwards
__DIR__."/path/to/file";
Does this work (apache only, i believe)
require_once($_SERVER['DOCUMENT_ROOT'] . '/class/' . $classname '.php');
I have dumped Zend Framework files in
"home/hotbuzz/public_html/include/zend/"
My hosting : linux
I want to load it in my script. Whenever I load I get this error.
Some info: I asked about my Zend, to hosting guys they said its located in "usr/local/zend"
But I want to use this home/hotbuzz/public_html/include/zend/
I had added these lined in my PHP:
set_include_path(dirname(__FILE__).';'.get_include_path());
require_once 'Zend/Loader.php';
I get this error
Fatal error: require_once() [function.require]: Failed opening required 'Zend/Exception.php' (include_path='/home/hotbuzz/public_html/include;.:/usr/lib/php:/usr/local/lib/php') in /home/hotbuzz/public_html/include/Zend/Loader.php on line 87
I want to set include path in my PHP code and configure it (.htaccess).
As I said in your previous question. Do not use ';' but use PATH_SEPARATOR.
This is a PHP constant that represent the right separator for your system (semi-colon on windows and colon on linux)
set_include_path(dirname(__FILE__).PATH_SEPARATOR.get_include_path());
You were doing it right. You should call set_include_path in first lines of your main script (index.php) and then include/require zend framework files. Remember to rename your Zend Framework containing folder to 'Zend' (uppercase Z) to follow ZF naming conversions, then put your Zend folder in your include directory.
<?php
$newIncludePath = array();
$newIncludePath[] = '.';
$newIncludePath[] = 'include';
$newIncludePath[] = get_include_path();
$newIncludePath = implode(PATH_SEPARATOR, $newIncludePath);
set_include_path($newIncludePath);
// now include path is setup and we can use zend
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoLoad('Zend_Loader', true);
// the rest of the code
?>
If you put your Zend directory in your include path, and not the include directory (that contains the Zend directory), you may not use this:
require_once 'Zend/Loader';
instead you should use:
require_once 'Loader';
which is not a good idea. by using the Zend/* model, you will remember which files are included from Zend Framework and which files are you own. so just add the include directory to your include path.
You may have more success if you use auto_prepend rather than include...
php_value include_path /home/hotbuzz/public_html/include/zend/
php_value auto_prepend_file Zend/Loader.php
What do you get in the apache log on startup and execution with that?
you can attach the below code in the first line of your bootstrap.php:
set_include_path('.' . PATH_SEPARATOR . '../library' . PATH_SEPARATOR . '../application/models/' . PATH_SEPARATOR . get_include_path());