I am building a PHP mySQL site locally with WAMP.
in my WAMP www directory i have made subfolders for each website I am making. eg:
WAMP\www\site1\index.php
WAMP\www\site2\index.php
WAMP\www\site3\index.php
etc
my DB connection is stored in:
site1/connections/open.php
my PHP scripts are stored under:
site1/php/filename.php
when i am running a script i need to include the open.php connection. the only way i can get this to work is by using:
include '../connections/open.php'; // this goes at the top of the PHP script
I know that if i use this and then move to remote servers or move some directories around I will have problems accessing this file. I therefore want the path to be relative to the basedir or baseurl(/site1/).
I understand that when you use an include it is looking for a dir and not an url so I cant use anything like;
$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
How do you go about sorting this out? Thanks
Yes you are right on that URL part
I understand that when you use an include it is looking for a dir and
not an url so I cant use anything like;
$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
How do you go about sorting this out? Thanks
You should try like this
$_SERVER["DOCUMENT_ROOT"]."/myFolder/"
Or try this
getcwd()."/myFolder/"
But there is a difference between both of them [Try Googling]
I think when you move to remote servers or move some directories around you will have no problems,because you are using relative path.
I think you should not use "$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']" and I think you should know what they mean!
Related
I'm developing a website in PHP.
The test environment is in a subdirectory of the root and there are multiple test environments. But the website doesn't work properly because of al the absolute and also the relative paths.
So i need a clean solution to get the path of the root of the website and not of the server.
I've tried a lot of thinks but nothing seems to work.
I hope you have a beter solution for this problem.
Thanks in advance!
Not sure if this will help, but if you want to get directory of current php file you should use __DIR__ constant. More info here.
If your website index is located at /home/www/subpath/index.php, this code in index.php:
echo __DIR__;
will return:
/var/www/subpath
You can use a SESSION to set your desired PATH and use it on all the path you need.
If you are using a framework like Laravel this can be done a lot easier.
Here's my problem using the PHP function dirname()
Let me first explain, I'm working on a website where I defined a constant as follow :
define('ROOT', dirname(__FILE__));
That worked well on a shared hosting, but I had to move the website on a dedicated server and now some files I try to call with an absolute link return 404. When I look what path they use, something like this appears :
http://myIp/var/www/myWebsite/[...]/image.jpg
What happens is that, my 'ROOT' constant is '/var/www/myWebsite' but in place of 'Replacing' my domain, it's added at the end of it. Which makes no sense since my Apache VirtualHost sends all requests towards 'myIp' to the local folder '/var/www/myWebsite'.
I don't know if the mistake is either from my PHP code, or my Apache VirtualHost.
Thank you for your help, I'm sure it's something stupid but I can't figure what I did wrong :)
I think you are confusing server paths with site paths.
You would not want to use dirname to give you a path for a web asset.
What you could do if you really want to do it this way is to remove the path to your web root.
This might work:
define('ROOT', str_replace('/var/www/myWebsite/', '/', dirname(__FILE__)));
I am developing a website on a local test environment. From time to time I need to import classes or functions into my php pages. At first I used relative paths to import files, but for some (unexplicable) reason the PHP couldn't find those files. So I have thought it would be necessary use the following:
<?php require_once($_SERVER['DOCUMENT_ROOT'] .'/mywebsite/inc/libs/functions.php'); ?>
The problem is when I have to upload all the pages to my remote webserver, the PHP interpreter won't find functions.php in that position because there is no mywebsite subfolder , on the other hand I can't get rid of mywebsite subfolder, because that would leave me with http://localhost/inc/libs/functions.php which leads nowhere.
So that basically means I will have to manually readjust the path to make everything work. My question is: is there a way for PHP to detect the exact folder of my website so that I don't have to change paths everytime I need to upload a php file to my webserver?
What is the reason behind impossibility of use relative path?
<?php require_once(dirname(__FILE__).'/inc/libs/functions.php'); ?>
Set the base folder with all the files you want as an include path in php.ini.
That way, when you use include () or require (), it will automatically look in the included path.
www.php.net/manual/en/ini.core.php#ini.include-path
I am working on a php project and I am having problems with including files.
I have a php script which is located at myapp/reports/index.php. When a form is submitted it performs an ajax post to another phpscript located at myapp/reports/phpHander/submit.php.
Submit.php then has to include a php script which is used to send an email. This is done to ensure that same code can be used over and over again without it needing to be typed for each time it is need. This php script is located at ../../administrator/classes/send.php.
Up to this point is working fine however the send.php script includes another file to get app config settings which is located in administrator/appConfig.php. The problem is this appConfig.php isn't being found even if I put in the full web address of http://localhost/myapp/administrator/appConfig.php.
What am I doing wrong. I am using the php include function to do this and its working for everything else but it seems to have a problem then including another script from a different location.
Thanks for any help you can provide.
http://localhost/myapp/administrator/appConfig.php is only URL path.
You need absolute filepath like C:/wamp/www/myapp/administrator/appConfig.php (Windows) or /var/www/myapp/administrator/appConfig.php (Linux)
Anyways best way is make a file "dirs.php" in your root application directory with constant:
define('ROOT_DIR', dirname(__FILE__));
where dirname(__FILE__) will be absolute path to your app directory.
With this knowledge you can include files in this way:
myapp/reports/index.php:
require_once('../../dirs.php');
include(ROOT_DIR . '/administrator/appConfig.php');
myapp/reports/phpHander/submit.php.:
require_once('../../../dirs.php');
include(ROOT_DIR . '/administrator/appConfig.php');
When you include another PHP script, all the paths are relative to the calling script. So, it sounds as though your script is at myapp/reports/phpHander/submit.php and includes ../../administrator/classes/send.php, which then includes another script in that same directory. In this case, you need to use the path "../../administrator/appConfig.php". Alternatively, you could use absolute paths relative to the filesystem's root.
So I had an issue last night when I went to upload a project I've been working on locally to my server.
I had this:
require_once "/../controllers/source/MySpaceID/myspace.php";
Which is the correct path to that file finding its way out of the libraries folder. It worked fine until I put it on the server. Any thoughts?
Echo you current working directory on the server ( echo getcwd(); ) and check your path from there, that's probably where your problem lies.
It sounds strange to me that CI would use ./library as its working directory - unless you set it yourself.
Note that you should use code igniter's APPPATH constant to create absolute paths instead of using relative ones, this will make things easier for you on the long term.
Lepidosteus is correct. Your library should live in /application/library/ and you should require it like this require_once APPPATH.'/libraries/MySpaceID/myspace.php'