Different behavior of PHP’s include/require on Windows and CentOS - php

These are code of my PHP applications myApp.phpand mypartsA/main.php.
myApp.php
<?php
echo ("##### myApp is starting now !!! </br>");
require_once('/parts/mypartsA/main.php');
?>
mypartsA/main.php
<?php
echo ("#### mypartsA is required </br>");
?>
And myApp.php and mypartsA/main.php are in following structure.
Document_root
|
+--- test
| |
| +--- myApp.php
|
+--- parts
|
+--- mypartsA
|
+---main.php
I’ve used this application on Windows+Apache for a year with no problem but recently I migrate this to CentOS+Apache then I get fatal error related to include_path
PHP Warning: require_once(/parts/mypartsA/main.php)
[<a href='function.require-once'>function.require-once</a>]
: failed to open stream
: No such file or directory
in /var/www/html/test/myApp.php on line 12
PHP Fatal error: require_once() [<a href='function.require'>function.require</a>]
: Failed opening required (/parts/mypartsA/main.php’
(include_path='.:/php/includes:/usr/share/pear:/usr/share/pear/PEAR:/var/www/html')
in /var/www/html/test/myAPP.php on line 12
According to PHP.net if a path is defined the include_path will be ignored.
http://www.php.net/manual/en/function.include.php
I think in my case PHP tries to find parts directory from C:\ or / not from include_path.
require_once(/parts/mypartsA/main.php);
Therefore result of myAPP.php must be fatal error whether on Windows or on CentOS.
But my application has been working for a year with no error on Windows. PHP can find parts directory correctly.
Can sameone try those codes above on Windows and see..
##### myApp is starting now !!!
#### mypartsA is required
or get error?
Now,I want to know is why results of same code are different between running on Windows and running on CentOS.
What dirctive in PHP.ini do I have to check ?
I know if I change this code to add absolute path with $ _SERVER['DOCUMENT_ROOT'].
require_once($_SERVER['DOCUMENT_ROOT']./parts/mypartsA/main.php);
It’ll work well. But there are many applications with same style of coding and I don’t want to change all of them.
These are additional information.
include_path values phpinfo() shows on Windows
.;C:\PHP;C:\PHP\pear;C:\Apache2.2.22\htdocs
include_path values phpinfo() shows on CentOS
.:/php/includes:/usr/share/pear:/usr/share/pear/PEAR:/var/www/html

$path = realpath(dirname(__FILE__)) . "/../../parts/mypartsA/main.php";
require_once($path);
try with this path
$path = "../parts/mypartsA/main.php";
the second path should work with document root.

Related

require_once: No such file or directory 'HTTP/Client.php';

I am working on ubuntu 12.04. I installed HTTP_Client by sudo pear install HTTP_Client. But when I am using require_once 'HTTP/Client.php';. It's showing:
Warning: require_once(HTTP/Client.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream: No such file or directory in /var/www/mai.php on line 3
How I remove this error?
How paths are resolved when using include or require is controlled by the include_path php.ini setting, which is typically set to:
include_path=".:/usr/share/php"
Whereby /usr/share/php points to where PEAR is installed.
It can be set using set_include_path() during runtime as well:
set_include_path(".:/usr/share/php");
Should be run before you include anything else.
'HTTP/Client.php' is a relative path. The error message means that the file doesn't exist at that location. You'll need to modify the path, or move the file.
In this case your PHP file is in "/var/www/" and there is no HTTP directory there.
Edit: I would recommend modifying the path to be the full path rather than the relative path:
require_once 'HTTP/Client.php';
Change to:
require_once '/usr/share/php/HTTP/Client.php';

PHP include with ../ failed open

I am trying to include a php file from the parent directory and I getting error:
admin#webby:~$ /usr/bin/php
/var/phpscripts/email_parser/tests/_email-test.php PHP Fatal error:
require_once(): Failed opening required '../PlancakeEmailParser.php'
(include_path='.:/usr/share/php:/usr/share/pear') in
/var/phpscripts/email_parser/tests/_email-test.php on line 6
Fatal error: require_once(): Failed opening required
'../PlancakeEmailParser.php'
(include_path='.:/usr/share/php:/usr/share/pear') in
/var/phpscripts/email_parser/tests/_email-test.php on line 6
PHP file
#!/usr/bin/php
<?php
error_reporting(E_ALL ^ E_NOTICE ^E_WARNING);
ini_set("display_errors" , 1);
require_once("../PlancakeEmailParser.php");
// etc
?>
Folder Structure
admin#webby:/var/phpscripts/email_parser$ find .
.
./composer.json
./README.txt
./LICENSE.txt
./PlancakeEmailParser.php
./tests
./tests/_email-test.php
For testing it works fine when I move PlancakeEmailParser.php into the tests directory and remove the "../" from the require
The line
require_once("../PlancakeEmailParser.php");
Is not using a fully qualified file-system path or PHP-URI-scheme. Therefore its outcome depends on PHP configuration, most often because of the include directory configuration.
The PHP CLI can use a different configuration file than with your webserver - or - the working directory is different as with your webserver (probably the later plays more of a role in your specific scenario).
What you want can be easily expressed fully qualified, too:
require_once(__DIR__ . "/../PlancakeEmailParser.php");
This is probably what you're looking for.
Quote from PHP CLI SAPI: Differences to other SAPIs:
It does not change the working directory to that of the script. (-C
and --no-chdir switches kept for compatibility)
In order to keep relative paths in your script working as-is, change directory to where the script resides, then execute the script:
cd /var/phpscripts/email_parser/tests/ && ./_email-test.php

Class 'PharData' not found

I get this error on my production server (CentOS 5.4 and php 5.3.5) :
Warning: include_once(PharData.php): failed to open stream: No such
file or directory in /var/www/ZendFramework/library/Zend/Loader.php on
line 146
Warning: include_once(): Failed opening 'PharData.php' for inclusion
(include_path='/var/www/fw:/var/www/vmms:/var/www/ZendFw/library:.:/usr/share/pear:/usr/share/php')
in /var/www/ZendFw/library/Zend/Loader.php on line 146
Fatal error: Class 'PharData' not found in
/var/www/vm/app/Backup.php on line 40
And this is the code which fail :
$phar = new PharData($imageBackupFile);
$phar->buildFromDirectory($imageDir);
Logger::info("Image directory backed up to: $imageBackupFile");
This code is working fine on my own computer.
PharData should be included by default in php 5.3+ ...
Thanks for your help!
UPDATE :
I am using the Zend Auto loader feature to load the good php files using this code :
require_once("Zend/Loader/Autoloader.php");
$autoloader = Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
Zend autoloader is doing the include_once(PharData.php).
Just because Phar is bundled by default in PHP 5.3 doesn't mean that it's necessarily included in your install. When you build PHP with ./configure, you can pass the --disable-phar to disable the Phar extension.
To confirm this, run the following script:
<?php
phpinfo();
?>
One of the first sections to appear will be the Configure Command section. Review this section to see if the --disable-phar switch is present, and if there is a Phar section to the page in general.
If it's not present, you'll need to contact your host to have it enabled. There's a decent chance, however, that they won't do it for you since it could impact other users depending on how their servers are set up. If this is on your own machine, you'll need to either rebuild PHP without that switch, or install Phar manually from PECL (no idea if this would still work in 5.3, but I don't see why it wouldn't).

Why can't I configure Zend-framework

Racking my head as to why I can't include the Zend-framework into my project. The documentation is unclear about how to set up the environment (Or haven't found where it is). Looked at this thread: How to configure Zend Framework on linux machine? but no luck. In the phpinfo() I see Zend configured but why can't I use it? Looked for documentation on how to add this to PEAR/PECL, no luck.
I did see this post: http://devzone.zend.com/article/4683 but still not working
What I have tried:
Option 1
require_once('/usr/share/php/libzend-framework-php/Zend/Loader/Autoloader.php');
Zend_Loader_Autoloader::getInstance();
Option 2
sudo vi /etc/php5/apache2/php.ini
changed this line:
;include_path = ".:/usr/share/php"
to:
include_path=".:/usr/share/php:/usr/share/pear:/usr/share/php/libzend-framework-php"
restarted Apache
Getting this error:
Warning: require_once(Zend/Loader.php): failed to open stream: No such file or directory in /usr/share/php/libzend-framework-php/Zend/Loader/Autoloader.php on
line 24
Call Stack:
0.0002 61600 1. {main}() /var/www/webservice/zend/server.zend.xmlrpc.php:0
0.0009 174412 2. require_once('/usr/share/php/libzend-framework-php/Zend/Loader/Autoloader.php') /var/www/webservice/zend/server.zend.xmlrpc.php:2
Fatal error: require_once(): Failed opening required 'Zend/Loader.php' (include_path='.:/usr/share/php:/usr/share/pear') in /usr/share/php/libzend-framework-p
hp/Zend/Loader/Autoloader.php on line 24
Call Stack:
0.0002 61600 1. {main}() /var/www/webservice/zend/server.zend.xmlrpc.php:0
0.0009 174412 2. require_once('/usr/share/php/libzend-framework-php/Zend/Loader/Autoloader.php') /var/www/webservice/zend/server.zend.xmlrpc.php:2
i would suggest to
chmod 775 /usr/share/php/libzend-framework-php -R as Ran BarZik suggestion .
and double check the apache user's permissions .
then symlink it to the your application library by using this :
ln -s /var/www/webservice/library/Zend /usr/share/php/libzend-framework-php/
I;ve installed Zend FW on many Linux system and if I encountered problems it was on the include path that was set incorrectly or permission problem.
Maybe the path isn't right, try 'vi /usr/share/php/libzend-framework-php/Zend/Loader/Autoloader.php' or any other text editor that you like. Does it work?
Maybe it is permission problem, try to change /usr/share/php/libzend-framework-php/ to 755:
chmod 775 /usr/share/php/libzend-framework-php -R
Your main configuration should be in: application/configs/application.ini
This tells the zend controller where to look for the library files etc.
Did you look here when you were installing? : http://framework.zend.com/manual/en/learning.quickstart.create-project.html
According to the error messages, your updated include_path isn't being picked up.
What does phpinfo() have to say about your include_path?
What if you set_include_path(get_include_path . ':/usr/share/php/libzend-framework-php'); before your include?
I'm assuming here that /usr/share/php/libzend-framework-php/Zend exists...
Try to move zend library:
cd /usr/share/php/libzend-framework-php
mv library/Zend /usr/share/php/
revert include_path to original:
include_path = ".:/usr/share/php"
Restart apache
After banging my head up against the wall for a couple of days I think I figured it out. Looking at this page: http://devzone.zend.com/article/4683 I understood I needed to add the zend path in the php.ini file. I did this with no effect. After looking at my PHP Configuration I noticed in /etc/php5/conf.d/ I could load zend by uncommenting the library path in the zend-framework.ini file which PHP loads as an additional module/ini

PHP Extension (memcache|memcached) not showing in phpinfo() but showing in php -m and php -i?

I am getting both modules listed as installed / configured when I use:
php -m
or if I use:
php -i
but when I use:
$m = new Memcache;
// or
$m = new Memcache();
// or
$m = new Memcached();
//or
$m = new Memcached;
I get the following error:
Fatal error: Class 'Memcached' not found
I am running on a Mac - OS X (10.5.7) with default install of apache & php. Additionally, I have memcached running as a daemon on 127.0.0.1:11211 and libmemcache as required by the php-memcached library. I have restarted apache tons of times and even done a machine restart.
Does anyone know why the modules/extensions show up in the command line but not in my phpinfo()? I am literally stumped, after 3 hours of googling, I am just about ready to give up.
Also, please note, my phpinfo() outputs my ini files as follows AND they are both the exact same file:
Configuration File (php.ini) Path: /etc
Loaded Configuration File: /private/etc/php.ini
UPDATE:
Apache is failing to load the extension.
[Fri May 14 04:22:26 2010] [warn]
Init: Session Cache is not configured
[hint: SSLSessionCache] PHP Warning:
PHP Startup: Unable to load dynamic
library
'/usr/lib/php/extensions/no-debug-non-zts-20060613/memcached.so'
- (null) in Unknown on line 0 PHP Warning: PHP Startup: Unable to load
dynamic library
'/usr/lib/php/extensions/no-debug-non-zts-20060613/memcache.so'
- (null) in Unknown on line 0
Does anyone know why or how this would happen? Both of the files referenced above DEFINITELY ARE there. Should I move this question to server fault?
Your webserver is probably using mod_php, which is a seperate bit of code from the standalone (CLI) interpreter. If they are both using the same ini file, and the memcache extension is configured in the ini file, then it sounds like for some reason, the mod_php is failing to load the extension - check your webserver error_log for startup errors.
It may be that the mod_php was compiled without memcache support (most extensions need to have a stub file linked into the php code, even though the bulk of the code is not linked until run time). Or it may be a permissions problem on the shared object file. Or your webserver may be running chroot, and unable to find the extension (which would also mean that although the ini files appear to have the same path, this is relative to different roots).
HTH
C.
because both versions use different php.ini
place your php.ini into location noted in the phpinfo() outout
I would suspect that the issue revolves around permissions. When you run php from comand line, it runs as the user invoking it. When run as an apache module, it runs as "nobody".
I would assume that the memcached.so file, or the directory it's in does not have proper permissions.
I stumpled upon this post and was having the exact same problem with an extension in my php -i but not in phpinfo(). Mine was a permissions problem because of selinux on a CentOS machine. I had to change ownership and permissions and now it is working as expected.
Set php path in environment variables as given below.
Right-click on "My Computer"
Properties
Advanced Tab > Environment Variables
Under System variables, scroll down to find "Path", select and click on Edit.
Add path to your php install on the end (make sure to precede with semi-colon ";"). Example: ";C:\php7"
Click Ok.

Categories