I just copied the library folder on the root directory of my net server . i.e. /var/www .
and used
require ( 'GChartPhp/gChart.php' ) ;
in my code on the file graph.php.
ERROR : But, for some reason , the browsers do not load this (graph.php) file .
However, I did the same on my local wamp server and my I am able to run the file.
I have no idea what I need to additionally do for this linux / apache server .
Thanks
I suppose the include_path is not configured the same way on your two servers : you might have to add either the WAMP equivalent of /var/www or . to it.
Modifying the include_path can be done by editing your php.ini file, or using set_include_path() at the beginning of your scripts.
Another possible solution would be to use an absolute path to the file you are including -- which can be done using something like this :
include dirname(__FILE__) . '/GChartPhp/gChart.php';
Notes :
__FILE__ will correspond to the full path to the file in which you write it
And using dirname() on it will get you the path to the directory which contains that file.
Which means that this line will use an absolute path... but written relatively to the file in which you put that line.
Related
I am attempting to move an old Intranet site running on Apache 2.2, to a WAMP setup (latest version) on my local machine.
One of the issues I have currently, is a require_once path failing to open, and I cannot determine what the cause is.
I have WAMP installed in:
C:\Wamp
I have changed http.conf and vhosts.conf to change the document root from
C:\Wamp\www
to
C:\Wamp\www\Intranet
This folder contains an index.php, which I can see being loaded correctly when browsing to localhost.
Index.php has an iFrame that loads welcome.php from
/site/welcome.php
This works, as the iFrame loads, but throws a 500 error.
Enabling PHP errors, the welcome.php page in the iFrame gives me an error on a require_once. The require_once is:
/site/login/config.php.
As you can see, I am using absolute paths here, so the fact that index.php is able to load /site/welcome.php, tells me it is loading the correct file from:
C:\Wamp\www\Intranet\site\welcome.php
I would expect then, my require_once with an absolute path to be loading:
C:\Wamp\www\Intranet\site\login\config.php
Which is a valid file path.
What is confusing me, is that the first absolute path I am using, seems to be starting from the document root, not the physical directory root.
The second absolute path I am using, does not seem to be starting from the document root.
Even more interestingly, if I change the require_once from:
/site/login/config.php
to
/login/config.php
It works?! I wouldn't expect it to, as that would suggest the absolute path I am specifying, is in fact a relative path?
I don't think this path (/site/login/config.php) makes sense on a windows machine.
Better than hardcoding the full file path, determine it on runtime.
E.g.:
define('ROOT_DIR', realpath(dirname(__FILE__)));
// because *nix and Windows path separators aren't the same (/ vs \)
define('DS', DIRECTORY_SEPARATOR);
// just for convenience sake, you could use DIRECTORY_SEPARATOR on its own.
Then including your config file, from welcome.php would be:
require_once(ROOT_DIR . DS . 'login' . DS . 'config.php');
I think that besides you are confusing the path that you use in the iframe declaration (which is a url path) with the path you use in the require (which is a filesystem path).
How i tend to resolve most of my include/require errors is by setting a variable path:
$path = dirname(__FILE__);
include($path.'/file.php');
This will prevent you from having trouble with different environments in this case being windows and linux.
Or even better:
define('ROOTPATH', realpath(dirname(__FILE__) . '/'));
require ROOTPATH.'login/config.php';
Another thing, probably not causing your issues, but being good practice is to set the DIRECTORY_SEPaRATOR.
define('DS', DIRECTORY_SEPARATOR);
include(DS.'home'.DS.'www'.DS);
This will make PHP use the correct slash (either / or \)
I want to play with excel sheet in PHP. So i found out PHPExcel provides that option.
But i am having a problem in setting up of the PHPExcel in my directory.
It says:
Extract and copy Classes to your includes/libraries directory. On your script, the minimum code you need to do is:
require_once dirname(__FILE__) . '/PHPExcel/PHPExcel.php';
I am not running PHP locally on my computer, So i do not have access to PHP remote directories.
What should i do?
This assumes you have a folder to upload to on a webserver that is running PHP:
I think you're a little confused by the way you have worded your question.
Your includes / libraries directory is one of your choosing, you can place the scripts anywhere and then use require() / require_once() to include the classes in the script you would like to use their objects in.
To break down the example:
require_once dirname(__FILE__) . '/PHPExcel/PHPExcel.php';
dirname(__FILE__) - FILE is a constant for use with any script. i.e. it is declared by the base PHP files not any scripts you create. It returns the absolute path to the current PHP script running (in relation to the document root). using dirname() evaluates FILE and returns the path without the script file location.
i.e.
dirname(__FILE__) = c:/docs/www/root/index.php
would be evaluated as:
c:/docs/www/root
So to wrap this up, place the documents in a directory above your web root folder. And require them from there.
Hope that helps.
Supposing you are in the root directory / of your host.
You have to upload PHPExcel's classes in a directory, usualy /PHPExcel/
Then, if your script is at the root of your host, you just have to add require_once dirname(__FILE__) . '/PHPExcel/PHPExcel.php'; just after your php opening tag.
I am using xampp to develop my php application. Few days back I installed pear ti use DB abstraction. After that, I couldn't use include files from parent directory, however I can include from sub-driectories.
Here is what I see when I check my include path
.;E:\xampp\php\PEAR
I tried changed include path using set_include_path to the location where my files are stored, then the application failed to load Pear files.
Any help appreciated.
Easiest way to prepend to the include path stack is...
set_include_path(implode(PATH_SEPARATOR, array(
'path/to/app/includes',
'path/to/any/other/includes',
get_include_path()
)));
If you really want to use set_include_path, you can do it like this:
set_include_path(get_include_path().PATH_SEPARATOR.'path_to_parent');
Use the predefined constant DIRECTORY_SEPARATOR in case your code moves to a server that uses a different directory separator.
Personally if I needed to set the path specially for a particular site, I would try to set the path in the .htaccess file in the site's web root. It provides a more obvious place to look for site-wide configurations like the include_path. Here is the line you would put in the .htaccess file:
php_value include_path ".;E:\xampp\php\PEAR;path_to_parent"
or on a Linux server:
php_value include_path ".:some_path/PEAR:path_to_parent"
Since I develop on localhost but deploy elsewhere, and since I don't want to have to force my sites to be under a Windows partition's root directory (currently F:\web_dev\htdocs), code like this:
require_once($_SERVER['DOCUMENT_ROOT'] . '/projXY/database/database_common.php');
OdbcExec($sql); // defined in the file above
causes Netbeans to issue a "Warning: unknown function".
Now, I could get round this by using a directory structure like :
F:\project_1
F:\project_2
instead of
F:\web_dev_htdocs\project_1
F:\web_dev_htdocs\project_2
and then using
require_once('/database/database_common.php');
BUT that imposes constraints on where the end-user an install my site.
Simplest by far would be to tell NetBeans which local directory corresponds to $_SERVER['DOCUMENT_ROOT'], but I can't find a configuration option for that. I am sure this is a common problem. Any suggestions?
Update: NetbBeans v7.0.1
Simply add the /path/to/projXY/database/ directory to your project's include path. Netbeans will then pick up the files there and use them as code references.
http://netbeans.org/kb/docs/php/project-setup.html#phpIncludePath
Addendum
Relying on $_SERVER['DOCUMENT_ROOT'] is generally a bad idea. For one, it eliminates the ability to run parts of your application via the console / command line.
You should instead either use configurable, absolute paths to shared libraries or do as in Brandon's answer and use a relative path from __DIR__ (PHP 5.3) or dirname(__FILE__)
Not sure if this is what you are looking for or not, but I commonly use:
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'file.php');
I'm deploying from my WAMP testing environment to an online test...
Locally I had my include paths something like this:
include('C/wamp/www...')
how do i find the equivalent path on my server?
i've tried using '/' to get to the root but i get this error:
Warning:
require_once(/test123/mvc/views/txt/index_nav_txt.php)
[function.require-once]: failed to
open stream: No such file or directory
in
/home/user/public_html/test123/mvc/views/components/st_footer.php
on line 37
Fatal error: require_once()
[function.require]: Failed opening
required
'/test123/mvc/views/txt/index_nav_txt.php'
(include_path='.:/usr/lib/php:/usr/local/lib/php')
in
/home/user/public_html/test123/mvc/views/components/st_footer.php
on line 37
You would actually need:
require_once("/home/codlife/public_html/test123/mvc/views/txt/index_nav_txt.php");
notice the edition of /home/codlife/public_html/
The initial / Takes you to the root of the server and your code is located inside /home/codlife/public_html/
do you mean
$_SERVER['DOCUMENT_ROOT']
which basically gives you the full path to your working website directory i.e. c:/wamp/www/(windows) or /var/www/vhost/domain.com/httpdocs/ (linux)
You should probably read up on include_path ( http://php.net/include_path ) - This is generally set to include the document root (where your website is) and can be altered so that you don't have to repeatedly include the same paths.
how do i find the equivalent path on my server?
You don't find it - you tell it where it should be. Admittedly this is not always practical when you buy a hosting package (which IME are usually badly supported and come with virtually no documentation).
First thing to note is regardless of where / how the code is hosted, you should always use paths relative to the directories configured on the php include path (or relative to the PHP script initially invoked by the browser request - the '.' entry from the include_path cited in the error) - never absolute paths. You can easily find this out with:
<?php
print ini_get('include_path');
?>
Judging from the path cited in the error message, it appears to be a POSIX system. The root of the filesystem as seen by the webserver might be quite different from the root as seen from your FTP or SSH software, but they are probably the same.
Note that if this is a shared host, then you probably won't have access to put files in /usr/lib/php or /usr/local/lib/php - so your only option is to use a relative path - which is going to get very messy -
You could do some clever coding around this - but do have a look at packages such as Dokuwiki and phpmyadmin to see how they organise the include files in a relocateable way without any dependance on manipulating the php.ini settings.
Alternatively you may be able to override the include_path via .htaccess, e.g.
php_value include_path ".:/home/codlife/public_html:/usr/lib/php:/usr/local/lib/php"
(which would set a base include_path to your document root)
HTH
C.
Use a configuration file where you store things like:
$application_root = '/home/code_life/public_html/';
In this file use all your environment specific variables or constants. When you deploy the application on a different machine, you just update configuration file.
Example:
You have in your root application a folder called settings with settings.php where you can define:
define('DIR_ROOT', dirname(dirname(__FILE__)) . '/');
Now, on every machine, the DIR_ROOT will be the root of your application and you don't have to change anything.