Intermittent error with include_once on shared php files - php

I have run into an annoying issue that seems to be popping up intermittently across my applications.
These applications live in /data/www/directory which is the php document root. All of these applications need to access a shared php file for SSO authentication which lives in /usr/share/php.
Even if I change the location of the file to /data/new/path, it will still fail.
When a file uses include_once "SSO.php";, it will sometimes fail, resulting in the following message:
Warning: include_once(): Failed opening '/usr/share/php/SSO.php' for inclusion (include_path='.:/usr/share/php') in /data/www/directory/path/to/index.php on line 2
I am using the default include_path in the apache2 php.ini which is where SSO.php lives.
The weird thing about this issue, is that if I open the file with file_get_contents(), I can var_dump the contents onto the screen. Example:
var_dump(file_get_contents('/usr/share/php/SSO.php'));
In the example I know that I'm using an absolute path. I have also tried to use include_once with the absolute path to the shared php file with the same error resulting.
Any suggestions would be excellent!

Related

PHP include: failed to open stream: no such file in "/literally/the/exact/location/of/the/file"?

So while trying to include a file in php, using include(/Users/leonaves/Sites/mysite/admin/inc/pages/dashboard.php) (I'm running locally), PHP tells me:
Failed to open stream: No such file or directory in /Users/leonaves/Sites/mysite/admin/inc/page_request.php on line 86.
I have tried including from a relative path, from an absolute path, declaring the root in a variable etc. etc. I have no idea how to make this file include the other. PHP refuses to find it AT THE EXACT LOCATION IT IS. Any help would be amazing, thank you.
PHP executes only scripts found in directories specified in open_basedir. Your path is probably not allowed.

FTP opendir() — "FTP server reports 550 [can't find file]"

I am attempting to read a open a directory using opendir(), and then loop through the files in that directory. The ultimate goal for this script is to transfer certain files from the directory that is being read to a different FTP server.
The "FTP" part is what's screwing me up—and I have no idea why!
On some servers (this script is expected to be run across several) I am receiving the following error:
Warning: opendir(ftp://...#jslsolutions2.flashsvr.com/streaming)
[function.opendir]: failed to open dir: FTP server reports 550
Can't find file in [PATH] on line 88
[PATH] would be the actual path to the PHP script, of course.
Line 88 is as follows:
if ($handle = opendir($from))
The variable $from represents a complete FTP path.
The complete FTP path is as follows:
ftp://[USERNAME]:[PASSWORD]#[SERVER]/streaming
I have confirmed that directory is correct. When I copy and paste the directory into my web browser, for example, Firefox opens a directory tree containing all of the files, exactly as expected. In other words, though PHP is complaining that it "can't find file", the "file" (that is the directory, I believe) actually does exist.
What on earth is going on? As I mentioned before, this script does work on some servers. The thing holding it up appears to be whether or not the server is Windows-based, as it works fine on the Linux servers I have tested. Unfortunately, I do not have access to the servers, though our server administrator can make changes if we can isolate the problem.
Why am I receiving this error, and is there a workaround?
Add a trailing slash / see if that resolves the issue there was a bug in PHP which has been fixed.

include_path error when trying to access app in localhost

I realize that a question thread already exists, but it has been closed since and the OP hadn't responded to the answer. The question thread can be found here: https://stackoverflow.com/questions/5547333/include-path-c-php5-pear
So, my problem is this:
I have my facebook app locally in my htdocs folder (my DocumentRoot) and it requires sdk/src/facebook.php. I receive the following error upon trying to view the page:
Warning: require_once(sdk/src/facebook.php) [function.require-once]: failed to open stream: No such file or directory in D:\Apache\Apache2\htdocs\www\immense-headland-4743\index.php on line 14
Fatal error: require_once() [function.require]: Failed opening required 'sdk/src/facebook.php' (include_path='d:\php\pear') in D:\Apache\Apache2\htdocs\www\immense-headland-4743\index.php on line 14
I have tried variations of the backslash and forward slash in my php.ini file; I have tried set_initial_path() and init_set() in my main index.php - but nothing works. What is going wrong here?
I failed to see the problem here because I was blindly following an existing code and did not look into it carefully. The solution was to look at the php.ini file and see where the include_path variable was pointing - it was pointing to my php installation folder while the php I required was in my apache folder. So I pointed the variable to that folder and now it works.
Courtesy:Cups in Trouble with local host PHP require_once
As he said : If you put your include/ folder into this folder;
/Users/jfloyd/IOD/includes/
Then alter this line in your PHP ini from:
include_path='.:/Applications/MAMP/bin/php/php5.4.10/lib/php'
TO
include_path='.:/Applications/MAMP/bin/php/php5.4.10/lib/php:/Users/jfloyd/IOD/'
Then save the file and restart your server, then the include files should be found.
This ini directive can be set in any number of places, in each file, in each folder using .htaccess or in httpd.conf or in the ini file, as I just described.

PHP Fatal Error Failed opening required File

I am getting the following error from Apache
[Sat Mar 19 23:10:50 2011] [warn] mod_fcgid: stderr: PHP Fatal error: require_once() [function.require]: Failed opening required '/common/configs/config_templates.inc.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/viapics1/public_html/common/configs/config.inc.php on line 158
I am definately not an expert of Apache but the file config.inc.php & config_templates.inc.php are there. I also tried navigating to a test.html page I placed in common/configs/ so I assume there is no rights issues going on. I also set the rights on config_templates.inc.php to give everyone read, write, and execute rights. Not sure what to do at this point, I checked to see if there was a /usr/share/php directory and I found there was not but when I did yum install php it said it had the latest. Ideas?
It's not actually an Apache related question. Nor even a PHP related one.
To understand this error you have to distinguish a path on the virtual server from a path in the filesystem.
require operator works with files. But a path like this
/common/configs/config_templates.inc.php
only exists on the virtual HTTP server, while there is no such path in the filesystem. The correct filesystem path would be
/home/viapics1/public_html/common/configs/config_templates.inc.php
where
/home/viapics1/public_html
part is called the Document root and it connects the virtual world with the real one. Luckily, web-servers usually have the document root in a configuration variable that they share with PHP. So if you change your code to something like this
require_once $_SERVER['DOCUMENT_ROOT'].'/common/configs/config_templates.inc.php';
it will work from any file placed in any directory!
Update: eventually I wrote an article that explains the difference between relative and absolute paths, in the file system and on the web server, which explains the matter in detail, and contains some practical solutions. Like, such a handy variable doesn't exist when you run your script from a command line. In this case a technique called "a single entry point" is to the rescue. You may refer to the article above for the details as well.
If you have SELinux running, you might have to grant httpd permission to read from /home dir using:
sudo setsebool httpd_read_user_content=1
Run php -f /common/configs/config_templates.inc.php to verify the validity of the PHP syntax in the file.
You could fix it with the PHP constant __DIR__
require_once __DIR__ . '/common/configs/config_templates.inc.php';
It is the directory of the file. If used inside an include, the directory of
the included file is returned. This is equivalent to
dirname __FILE__ . This directory name does not have a trailing slash
unless it is the root directory. 1
Just in case this helps anybody else out there, I stumbled on an obscure case for this error triggering last night. Specifically, I was using the require_once method and specifying only a filename and no path, since the file being required was present in the same directory.
I started to get the 'Failed opening required file' error at one point. After tearing my hair out for a while, I finally noticed a PHP Warning message immediately above the fatal error output, indicating 'failed to open stream: Permission denied', but more importantly, informing me of the path to the file it was trying to open. I then twigged to the fact I had created a copy of the file (with ownership not accessible to Apache) elsewhere that happened to also be in the PHP 'include' search path, and ahead of the folder where I wanted it to be picked up. D'oh!
you can set the include path in php.ini
include_path = ".:/home/viapics1/public_html"
I was having the exact same issue, I triple checked the include paths, I also checked that pear was installed and everything looked OK and I was still getting the errors, after a few hours of going crazy looking at this I realized that in my script had this:
include_once "../Mail.php";
instead of:
include_once ("../Mail.php");
Yup, the stupid parenthesis was missing, but there was no generated error on this line of my script which was odd to me

Why am I unable to use a variable in my require or include statement on XAMPP?

While cleaning up some old php scripts I've noticed some weird behavior with require/include statements when I try to use variables.
On the live server, the following code works fine..
<?php
$test = "http://localhost/elearning/trunk/mypage.php";
require "$test";
?>
..but on my XAMPP installation ((basic package) version 1.6.7) I receive the following error:
Warning: require() [function.require]: URL file-access is disabled in the server configuration in C:\Documents and Settings\username\Desktop\xampp-win32-1.6.7\xampp\htdocs\elearning\trunk\test.php on line 22
Warning: require(http://localhost/elearning/trunk/mypage.php) [function.require]: failed to open stream: no suitable wrapper could be found in C:\Documents and Settings\username\Desktop\xampp-win32-1.6.7\xampp\htdocs\elearning\trunk\test.php on line 22
Fatal error: require() [function.require]: Failed opening required 'http://localhost/elearning/trunk/mypage.php' (include_path='.;C:\Documents and Settings\username\Desktop\xampp-win32-1.6.7\xampp\php\pear\') in C:\Documents and Settings\username\Desktop\xampp-win32-1.6.7\xampp\htdocs\elearning\trunk\test.php on line 22
If I copy+paste http://localhost/elearning/trunk/mypage.php (directly from the error) into my browser, mypage.php loads. Is this an error in my configuration, or my approach?
You can't use paths that start with http:// on some servers because of security. What you want to use instead is a directory path.
You can get your current directory path by doing something like
echo $_SERVER['DOCUMENT_ROOT'];
that will give you your directory path from the root folder of the server. That is the path you want to use to include/require stuff.
If you didn't quite understand that, try this.
There are two types of paths:
World Wide Web Paths:
http://example.com/directory/file.php
Server Directory Paths:
/home/usr/www/site/html/
The Server directory path is where your files are located on the server's hard drive. Much like your computer's hard drive, it is never changes unless you move the files. On the other hand, The World Wide Web Path (the one everyone uses to access your website) can change based on what domain you are using, where your Document Root is pointing, Mod Rewrites, and more.
Note: The Document Root is the top most directory that your server serves files from. So, if you had index.php in your document root, it would show up like this on the web:
http://example.com/index.php.
When PHP looks for a file, it uses the Server Directory Path, because it is running on the server. When Javascript, which runs from the user computer, wants to look for a file, it uses the World Wide Web path to access it, because it isn't accessing it from the server.
I really hope that somewhere in there, something made sense.
try require "./mypage.php"
not the whole exact directory
PHP isn't smart enough to know that you're including from the same server when you use URL notation like that, so it thinks you're grabbing files from a seperate server (which can be dangerous).
Your server configuration is set to disallow loading these external files (evident by the error message "URL file-access is disabled in the server configuration"), so it's failing.
Since you're on the same server, you can simply use relative paths, like:
require_once 'mypage.php'
Don't use a path like that ... use a relative path or absolute path.
Example
require 'mypage.php'

Categories