Solved

How to obtain the server real path in PHP programatically?

Posted on 2016-10-26
10
57 Views
Last Modified: 2016-10-26
Hi Experts

How to obtain the server real path in PHP programatically?

Ex. in local machine
$storeFolder =  'C:/xampp/htdocs/';

Open in new window


Thanks in advance
0
Comment
Question by:Eduardo Fuerte
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 41860149
Use this function to see the $_SERVER variables: http://php.net/manual/en/function.phpinfo.php

This varies some from release to release and is installation-dependent so you may want to make some tests.  But if the information is available anywhere, it's available here.  Look for PHP Variables near the bottom of the output.
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 41860158
$path =  __DIR__;

Open in new window


Will set the path to the script you are running this from.
0
 

Author Comment

by:Eduardo Fuerte
ID: 41860182
Hi


This $_SERVER['REQUEST_URI'];   (f.e.)
/meeta_ike/reclamacao/save

Open in new window


More adequated
 $storeFolder = dirname( __FILE__ );    
C:\xampp\htdocs\meeta_ike\application\controllers

Open in new window


Just one favor... how to obtain this:
'C:/xampp/htdocs/meeta_ike/

Open in new window

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 58

Accepted Solution

by:
Julian Hansen earned 400 total points
ID: 41860233
You probably want
$_SERVER['DOCUMENT_ROOT']

Open in new window

OR
$_SERVER['CONTEXT_DOCUMENT_ROOT']

Open in new window

Depending on your server.
The following are equivalent to each other
dirname(__FILE__) == __DIR__;

Open in new window

The following will give you the directory you run this in.
$storeFlder  = __DIR__;

Open in new window

What I would do is create a file called folder.php
<?php
function getStorageFolder()
{
  return __DIR__;
}

Open in new window

Then in the file where I want to know where the store folder is I would include it
require '../../some/path/to/storage/folder.php';
...
$storageFlder = getStorageFolder();

Open in new window

0
 

Author Closing Comment

by:Eduardo Fuerte
ID: 41860253
$storeFolder = $_SERVER['DOCUMENT_ROOT'].'/meeta_ike/public/uploads/anexos/';

Fits perfectly to my needs by now.

If something changes in terms of server I will probably reconsider the remaining code.

Thanks for the assistance!
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41860266
getStorageFolder()
I probably would not write a function to return a predefined magic constant unless there was some unit-testing mock requirement.  And I generally discourage unit-testing PHP.  I have plenty of bugs of my own -- no need to go looking for others! ;-)

Eduardo: Please print out the contents of var_export($_SERVER) and post it here in a code snippet, so we can be looking at the same things you're looking at.  As I wrote above, these variables differ from one installation to the next, and what I have on my servers is not likely to be what you have on your C: drive.

Also, if you can tell us why you need this information we may be able to suggest an alternative.  Any code that depends on file paths or directories is likely to have dependencies that make it less portable and more brittle.  So if there's another way to solve the issue that you're dealing with, it might be better to go another way.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41860337
<?php // find_my_url.php
/**
 * YMMV - This works on properly configured PHP installations
 *        but cannot be guaranteed on all PHP installations
 *
 * http://www.faqs.org/rfcs/rfc3986.html
 * http://php.net/manual/en/function.parse-url.php
 */
error_reporting(E_ALL);

$scheme = $_SERVER['REQUEST_SCHEME'];
$dlm    = '://';
$host   = $_SERVER['HTTP_HOST'];
$uri    = $_SERVER['REQUEST_URI'];

$url
= $scheme
. $dlm
. $host
. $uri
;

// PARSE AND DISPLAY THE URL
echo '<pre>';
echo PHP_EOL . $url;
echo PHP_EOL;
var_export( parse_url($url) );

Open in new window

0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 41860384
... except it does not give the absolute path - which as I understood it was what the question was about.

Returning magic constant's is not ideal - not sure where the concern over unit testing comes in - but the advantage with __DIR__ is it will tell you the full path of the file that contains the constant.

From a scoping perspective you don't want to global the value in - so having a function return it is a useful way to wrap it. The benefit of this approach is that you can use relative paths to require the folder script and it will tell you the absolute path for the folder it is in - which makes it portable.

I personally wouldn't use this mechanism on a production system but would use this to determine the path of a particular folder and then use that value in a config file.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41860400
I believe that Magic Constants are already global (all PHP defined constants are global; "magic" constants change in context).  The function does not encapsulate anything.
http://php.net/manual/en/language.constants.predefined.php
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 41860449
... yes but that is not useful. You need to see the value of __DIR__ within the context of a particular file. Calling it from File A just tells you were File A is. When called from with in File B it tells you where File B is - so you could just have File B
$location = __DIR__;

Open in new window

But that would mean
a) Potential name collision
b) Accessing via global

Hence putting it in a function ...
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question