Help with PHP and filepaths - php

I am using windows right now but I need my script to work on windows or linux. I am working on a project which allows to upload video to youtube, the youtube library requires the use of Zend framework (unfortunately) so I am really trying to get it to work, with no luck.
So my page says
Warning: require_once(Zend/Loader.php) [function.require-once]: failed to open stream: No
such file or directory in
E:\Server\htdocs\clients\youtube2\demos\Zend\Gdata\YouTubeVideoApp\operations.php on line 37
I have Zend framework located at
E:\Server\htdocs\frameworks\Zend
I then try to set the include path so that the scripts have access to Zend, thats where my trouble starts. I try to use this...
$path = 'E:/Server/htdocs/frameworks/Zend/';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
But I still get the error that you see above, now that you can see the path of my script above and the path of my Zend, can someone show me how to set the include path to work correctly? Thanks for any help
I tried some suggestions with no luck so far. Here is my updated code and result
$path = 'E:\Server\htdocs\frameworks';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
echo get_include_path();
Gives this...
E:\Server\php\PEAR;E:\Server\htdocs\frameworks
So it appears my include path for zend is added, but it still says it cannot find it
Final Update!
It is working, just where I set the include path wasn't getting included into all the files, is there a way to set the include path and have it be available globally?

If you want to change the include path in php.ini, just change include_path. Check out this tutorial to find out about a myriad of ways of changing the include path.

E:\Server\htdocs\frameworks\Zend
versus
$path = 'E:/Server/htdocs/frameworks/Zend/';
might make a difference on your system? Try it with \ instead of /.

Related

Fail with nexted includes

I have a script that includes a file and that file has an include in it, like this:
In script:
include('includes/functions/homepage.php);
In homepage.php:
include('includes/functions/parent_functions.php');
I searched here and see it is a very common problem and the solution seems to be to use
include(dirname(__FILE__) . '/includes/functions/homepage.php');
I have the above and still get this error:
Warning: include(includes/functions/parent_functions.php): failed to open stream: No such file or directory
I've tried this on a site running php 5.5 and another using 7.2 - fails the same on both. If I print the path using the following it shows the correct full path.
echo dirname(__FILE__) . '/includes/functions/homepage.php';
As mentioned, this is a common question here but the fix isn't working in my case. Can anyone see why?
Well, you have an issue here. As the parent_functions.php and homepage.php are in the same directory you need not put an extra directory prefix. You can simply use
include('parent_functions.php');
instead of
include('includes/functions/parent_functions.php');

what's wrong with my require_once path?

I've switched my files over from a local environment to my vps and now my facebook notification isn't working even thought I'm pretty sure I've updated all the paths correctly. I've tried writing the require path numerous ways.
I'm doing a "$.post" from jquery to a php page where the facebook notification is sent and am getting this error:
<b>Fatal error</b>: Class 'Facebook' not found in
<b>/home/zjkkvcxc/public_html/accepted.php</b> on line <b>9</b><br />
//THIS IS MY PHP REQUIRE PATH.
require_once('php-sdk/facebook.php') ;
//IN MY LOCAL ENVIRONMENT I WAS USING THIS PATH BECAUSE IT WAS THE ONLY ONE THAT WORKED. THIS DOESN'T WORK ON MY VPS THOUGH.
require_once(dirname(__FILE__).'/php-sdk/facebook.php') ;
You use require_once and have error "Class 'Facebook' not found". If you tried require_once on a file that does not exist, it would cause other error: "Fatal error: require(): Failed opening required 'php-sdk/facebook.php'". So the path is probably OK. Check if you uploaded php-sdk properly. The facebook.php might be empty.
Your error is :
Fatal error</b>: Class 'Facebook' not found in
<b>/home/zjkkvcxc/public_html/accepted.php</b> on line <b>9
The valuable information here is :
the error occurred because the Facebook class is unknown which means that require_once did not pop this error
the error occurred on line 9 of accepted.php
Now you have to look why the class Facebook is unknown on line 9.
if you have included the facebook.php before line 9 it is probably not containing the right class. (class names are case sensitive!)
if you include facebook.php after line 9 you just have to call it earlier.
PS: posting the first 10-15 lines of accepted.php might give us enough information to pinpoint the exact problem here.
I have faced issues like this before, and the best way to handle this is to set your true filepath as a variable & prepend that to your includes/requires. Becuase the whole dirname(__FILE__) setup can act oddly in different environments especially those that use symbolic links. Explicitly stating where files are to be set is the best solution.
So let’s assume this is your codebase path; as per your example:
/home/zjkkvcxc/public_html/
Set that as a variable that all of your pages load in some way like this:
$BASE_PATH = '/home/zjkkvcxc/public_html/';
And now when you make calls to the file system for your app, do this:
require_once($BASE_PATH . 'php-sdk/facebook.php');
What is nice about a setup like this is that you can make your app portable between environments by just changing $BASE_PATH to match your local environment. Like this might be a path for a MAMP (Mac OS X LAMP) setup:
$BASE_PATH = '/Application/MAMP/htdocs/';
Regarding how odd __FILE__ can act in symlinked environments, read up here:
Since PHP 4.0.2, _ FILE _ always contains an absolute path with
symlinks resolved whereas in older versions it contained relative path
under some circumstances.
Class not found error not refers to problem loading file. You are using a require , if file not exists a require error will be raised and this is not the case.
Probably the class inside facebook.php is not called Facebook, probably is with another name or with other case like "facebook"
Fatal error</b>: Class 'Facebook' not found in
<b>/home/zjkkvcxc/public_html/accepted.php</b> on line <b>9
Is it possible that:
your short_open_tag is enabled on your local environment and
it's disabled on your vps and
you are using <?instead of <?php in your facebook.php file
For some odd reason, i encounter circumstances where php doesn't find the file i need through conventional means. i found some code used for searching a directory and retrieving a list of files, which was advantageous in a url generation script i was writing. overtime, i have used this simple script for many tasks, such as finding files when normal require/include fails. in a pinch, this hack works for me.
Just fill in the file name $somefile and directory name $log_directory if your directory is in the base path just type it with no slashes. if its below the base path type it like this /path/to/folder
hope this helps.
$somefile = '<!--Filename You need to find-->';
$log_directory = '<!--Directory you want to search-->';
$results_array = array();
if (is_dir($log_directory))
{
if ($handle = opendir($log_directory))
{
while(($file = readdir($handle)) !== FALSE)
{
$results_array[] = $file;
}
closedir($handle);
}
}
foreach($results_array as $value)
{
if ($value == $somefile) {
$url_include = $log_directory."/".$value;
}else{
die("error file not found.");
}
}
require_once("$url_include");
require_once JPATH_SITE.'/php-sdk/facebook.php';
The require path you said is wrong:
require_once('php-sdk/facebook.php') ;
This need there has a file got full pathname: /dir/to/your/actual/include/path/php-sdk/facebook.php
And later you require file in include_path wrong, dirname(__FILE__) OR __DIR__ is directory of the file where require_once() is called, so you are looking for facebook.php in php-sdk sub-directory under you current php script.
To fix your problem, you have 2 plan
Better one, find your actual include_path and put php-sdk files in, include_path can find from php.ini, or run php -m from shell, or phpinfo(), in this way, your first require_once() will work.
Make sure php-sdk directory includes facebook.php is in same parent directory with your calling php script, this is not common usage, but will make your second require_once() work.
I suggest you to study more on php include_path configure and __FILE__ magic constant.
define('DOC_ROOT', realpath(dirname(__FILE__) . '/'));
require_once(DOC_ROOT . '/php-sdk/facebook.php');
Assuming the folder php-sdk is at the root of you website directory.
require_once('php-sdk/facebook.php') ;
This assumes that the facebook.php file is in a folder that resides in the current folder of the PHP file beind called.
Is this the case? I suspect not since you have an error!
Are you in a folder up from the root?
---ROOT
---ROOT > FOO --your script here?
---ROOT > php-sdk --facebook in here
If so...
require_once('../php-sdk/facebook.php');
would work.
Try $_SERVER['DOCUMENT_ROOT'].'/path to /php-sdk/facebook.php'
$_SERVER['DOCUMENT_ROOT'] will give absolute path for www or htdocs folder then you can provide path to facebook.php

Using PEAR XMLRPC2 inside wordpress plugin

I am trying to run the PEAR XMLRPC inside a plugin function to do some validation. It is all working fine in a standalone app that in a folder on my server, but as soon as I put all the files into my plugin folder, the:
require_once 'XML/RPC2/Client.php';
doesn't work. The Client.php file return an error:
Warning: require_once(XML/RPC2/Exception.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream:
So i assume it has to do with relative vs non-relative files, but I can't seem to figure out why it works outside of wordpress just fine.
If I start changing all the
require_once 'XML/RPC2...
to be the absolute path on the server the errors start going away, but I feel like I shouldn't have to change the source of PEAR and XMLRPC to do what i need. Any Suggestions?
You could update the PHP include path:
set_include_path(get_include_path() . PATH_SEPARATOR . $pathToPearLibs);
require_once($pathToPearLibs . 'XML/RPC2/Client.php');
This will give PHP one more (correct) place to look after it attempts to load from the wrong place.

How do I include a PHP script from a base path other than the current script?

I have a PHP application I'm trying to include, but it's causing problems because I'm trying to include its files from a subdirectory. For instance, here's the directory structure (more or less) I'm trying to work with:
/site
/scripts
/local
some_script.php
lib_file_a.php
lib_file_b.php
lib_load.php
lib_load.php has an include command for files lib_file_a.php and lib_file_b.php (relative path).
What I want to accomplish is to include lib_load.php from some_script.php, but there's a problem with the way I'm including lib_load.php. At first I tried require('../../../lib_load.php');.
As you would expect I saw the error, "Failed opening required 'C:\xampp\htdocs\site\scripts\local\lib_file_a.php'". As you can see, PHP is trying to include a file that does not exist. I found a Stack Overflow post that told me about using an absolute path (link), but this did not help. I next tried this code:
require(realpath(dirname(__FILE__) . '../../../' . 'lib_load.php'));
However, this time, the same error comes back. What am I doing wrong?
EDIT: I realized that the issue was actually in the library loader, which turned out to be including files based on an absolute path, not a relative path. (I should have read more carefully.) I resolved the issue by changing the path used by the library. This question should be closed if a mod sees this request.
To include lib load
require $_SERVER['DOCUMENT_ROOT'] . '/scripts/lib_load.php';
inside of lib load
require dirname(__FILE__) . '/lib_file_a.php';
if I got your problem right
Try this:
require dirname(__FILE__) . '/../../../lib_load.php');
Or if you're using PHP 5.3 or higher use this instead (it looks nicer):
require __DIR__ . '/../../../lib_load.php');

Zend Gdata include path issue (Loader.php)

I've been trying to install Zend Gdata. I'm running from a dev environment so have access to php.ini.
I've set the include path and when I run the verification script I get the following..
Ran PHP Installation Checker on 2011-04-28T02:25:20+00:00
PHP Extension Errors Tested
No errors found
Zend Framework Installation Errors Tested
No errors found
SSL Capabilities Errors Tested
No errors found
YouTube API Connectivity Errors Tested
No errors found
But when I try to run any of the demo files I get the floowing error...
Warning: require_once(Zend/Loader.php): failed to open stream: No such file or directory in /usr/lib/php/ZendGdata/demos/Zend/Gdata/blogger.php on line 37
Fatal error: require_once(): Failed opening required 'Zend/Loader.php' (include_path='.:/usr/lib/php') in /usr/lib/php/ZendGdata/demos/Zend/Gdata/blogger.php on line 37
The most logical conclusion is that there is a problem with the include path, but I have checked it and it seems right.
Here's what I have for it...
.:/usr/lib/php/ZendGdata/library/Zend:/usr/lib/php/ZendGdata/library/
Any suggestions would be greatly appreciated.
Put this in the beginning of Blogger.php
set_include_path('/usr/lib/php/ZendGdata/library' . PATH_SEPARATOR . get_include_path());
You say you're setting the include path in a configuration file but that doesn't seem to be affecting CLI. Make sure you're editing the right php.ini file with php --ini
$clientLibraryPath = 'ZendGdata/library';
$oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $clientLibraryPath);
if you do not know the root path of the server, use relative path for accessing library. Its pretty handy to use.
above two lines should be written on the top of any file{page1,page2,page3} having folder structure as below
Website
Page1.php
Page2.php
Page3.php
ZendGdata
You can use your relative path as per your need

Categories