Getting path from current working directory not from root - php

Hello everyone I am trying to get the path from the current working directory but I don't want the root path included with it in PHP or wordpress. For example I have a directory structure like this.
-C
--wamp64
--------www
-----------project
------------------Other directories
Now, what exactly I want is to get the path from project directory something like this.
-project
--Other directories
Currently with __DIR__ or dirname(__FILE__) I am getting.
C:\wamp64\www\project\other_directories
But I want to get.
\project\other_directories
And I wanted to make it dynamic so that it picks up the right path from which ever server I deploy this project.
I searhed alot but couldn't find the solution.

https://www.w3schools.com/php/showphp.asp?filename=demo_global_server
Check above URL and use $_SERVER['PHP_SELF'];
remove the filename from that and you will have the path relative to the document root.

Related

Relative Path Basics

Although I have a fair idea of relative path but I am a little confused while using this particular relative path in a program. Please someone guide me through. I have the following directory structure:
The problem here is that I want to include config.php file in left.php, for that I have given a relative path, include(../../layout/config.php); but somehow this is not being included and the relative path that I have to use is include(../layout/config.php);. Can someone please explain why is that so? Because my knowledge says that I will have to leave includes->admin and then enter layout.
In PHP it's the working direcory that is used as the main path.
All relative paths originate from that folder.
As you wrote in comments you use index.php which is one folder lower than left.
I assume you are in index and include left.php, this does not update the working directory.
Your working directory is still "admin".
You can find out what the working directory is using echo getcwd();
http://php.net/manual/en/function.getcwd.php
As I wrote in comments you can use include($_SERVER['DOCUMENT_ROOT'] . "/Electronix_Store/electronix/layout/config.php"); to use full path instead.
This is easier if the files and folders are in the same position all the time.
My advise is to only use relative paths if you are working with day "user/123/" -> some folder structure in there.

glob - set relative path root directory

I'm having some trouble with glob in php and although I did some research, I just couldn't seem to find an answer to my problem.
First of all to my directory structure:
/ (root)
/images (folder)
- gallery/ (sub folder containing image files)
/pages (folder)
- gallery.php
I want to access the subfolder 'gallery' in the folder 'images'. So: In my gallery.php file I got the following line:
$images = glob("/images/gallery/*.*");
And this doesn't work.
What does work is, if I change the path to "../"
$images = glob("../images/gallery/*.*");
or if I change the code to:
define('BASE', $_SERVER['DOCUMENT_ROOT']);
$images = glob(BASE."/images/gallery/*.*");
Unfortunatly the sourcecode then shows some info I'm not sure I want to actually make public
e.g.
/home/scecjwkh/htdocs/images/gallery/3.JPG
I hope the information I provided is enough to actually understand my problem. Not sure why I have so much trouble with a relative path o.O
Thank you in advance,
Stuben
What you usually do is use __DIR__ to build relative paths from the current script's folder. You can also use __DIR__ to know what part to cut from the final paths, because you should know where in the project your current file is and thus know where the root is relative to it.
$images = glob(__DIR__."/../images/gallery/*.*"); gets the image list without caring about current working dir, after which you can use realpath(__DIR__.'/..') to figure out how much to snip from each file path.

access root file from subdomain in PHP

Is it possible to include file from the ftp's root in a script in a subdomain ?
This doesn't work for me:
include($_SERVER['DOCUMENT_ROOT'].'/joomla.php');
Can someone help me ?
I think first you need to determine where is your subdomain files are placed and echo the $_SERVER['DOCUMENT_ROOT'] command to see the full path then you can combine two paths and make necessary adjustments.
Usually but not always, subdomain files are placed in a folder one level up then the main site's root folder so you may need to replace the folder name according to your needs in the output of server document folder function.
To give an example if your domain files are in a path like
/var/www/vhosts/example.com/httpdocs/
and if your subdomain path is like
/var/www/vhosts/example.com/subdomain/
then you will need to replace httpdocs with subdomain (or the other way around) in the output where you call $_SERVER['DOCUMENT_ROOT']

Saving to "/" on Apache server w/CodeIgniter

This seems like a basic question, but it's stumping me. I have CodeIgniter installed, and I've got a model that manipulates and saves XMLs. My problem is when trying to save it to "/" I get a "Permission denied" PHP error. I need to save them to a separate directory relatively, but I'm not sure where exactly "/" is located on the server. Is it in the "/www" of Apache, or root of the whole server? Once I know this I should be able to navigate to the correct directory
/ is the root directory. The starting point of your directory structure. This is where the Linux system begins. Every other file and directory on your system is under the root directory. Usually the root directory contains only sub-directories, so it's a bad idea to store single files directly under root.
Try specifying the complete path in your application.
Example: /home/user/public_html/yourApplicationFolder/
Or specify a relative path:
Example: ../somePath/.
This article could be helpful.
To get the filesystem path to the document root just use $_SERVER["DOCUMENT_ROOT"];

put config in secure place, can not link the files

Am trying to use a config file for a database and rating script but the problem is the config file is in this directory :
website.com/include/config.php aka websitename/include/config.php
The rating script needs the config and is accessed like this:
include_once("config.php");
I want the config to be in:
"/files/website/"
A directory level up from the website root folder.
I have been trying with:
"../files/website/" and other variations but can not figure out how to link them.
I have managed to put one config file and access it, but with this ajax rating script the only way for it to work is to have the config in the /include/ folder next to:
rating_process.php - has this link : include("inc/config.php");
rating_functions.php - has this link : include_once("config.php");
rating_total_functions.php - has this link : include("inc/config.php");
Hope i've explained myself here
Right, looking at my hosting now:
$_SERVER['DOCUMENT_ROOT']; outputs this: /foldy/homepages/11/username/htdocs/webbysite
My index file is located at: /foldy/homepages/11/username/htdocs/webbysite/index.php
The included rating script is located in: /foldy/homepages/11/username/htdocs/webbysite/include/
I want the config to be in /foldy/homepages/11/username/htdocs/secretfiles/config.php
Am trying to some how go out of: webbysite folder and then into secretfiles (sibling folders)
I have tried adding ../ and so on, but am missing something obviously :(
Try
$configLocation = $_SERVER['DOCUMENT_ROOT'].'../files/website/config.php';
include_once($configLocation)
but the problem is the config file is in this directory
it is not a problem at all.
just keep it as is.
Concerning your particular problem, your problem is that you don't know where you want to put your file. /files/website/ is not likely a right path and it is apparently not one level high from webroot.
So, first of all make your mind about the right path to the directory and it's relative position to the web root
if you are concerned about security ( because your config file contains the db details ) i would place the db config file outside the site root folder and then require_once('../../dbConfig.php') from the script that's creating xml or json for your ajax
more exactly ...
your site folder might be here: /var/www/html
set a virtual host (done differently on Linux and Windows) and point your domain to a sub folder inside /html so that the new path to the site root is /var/www/html/site.
then place your config file in /var/www/html and call it from your scripts inside your /site folder using require_once('../dbConfig.php)`.
your db details are outside the site folder

Categories