Included file's root path - php

how can i get "included file's root path" ?
d:\projects\project1\incs\inc.php :
In this file below line gives me "d:\projects\project1\incs"
<?php echo __DIR_; ?>
I want to get "/projects/project1"
I use that
str_replace("\\","/", str_replace(dirname(dirname(dirname(dirname(__DIR__)))), '', __DIR__))
but it's so complicated...
i want to use:
<img src="<?php echo ROOT_DIR ?>/images/image.jpg" />

If you are talking about the web-root, you can use:
$_SERVER['DOCUMENT_ROOT']
Edit: I just noticed your edit, and $_SERVER['DOCUMENT_ROOT'] is the root of your site relative to the file-system. It is not a value you want to use in html.

You're probably looking for pathinfo(), but if you want to be able to display images on your page, you'll need to use a URL rather than a file path. In that case, you'll want to use "http://" . $_SERVER['SERVER_NAME']

You can use
<img src="<?php echo $_SERVER['DOCUMENT_ROOT'];?>/images/image.jpg" />

Related

Change of folder path when require/ include an php file

I am currently using an index.php to include another file , it structure like:
root / index.php
/ new/ index.php
/ img/ test.jpg
And I write following in root/index.php :
<?php
require_once("new/index.php");
?>
the problem is , all paths in it are wrong. As all paths are based on new/index.php.
For example, In new/index.php my test.jpg is like
<img src="img/test.jpg" />
It shows http://www.test.com/new/img/test.jpg when I directly access new/index.php
But it shows http://www.test.com/img/test.jpg when I include the php in index.php.
How to fix it? I tried chdir() but it does not work expect Thanks
Make sure you always include with an absolute path, like:
require_once(dirname(__FILE__) . "/otherfile.php");
require_once(dirname(__FILE__) . "/../uponefolder.php");
require_once(dirname(__FILE__) . "/sub/folder/file.php");
Or use autoloading.
Did you just ask the same question twice? Use the required / included file as base directory in PHP
See my answer there.
The folder path you have in the require_once() function is relative to the directory your page is currently in. Have a look at the set_include_path function. Specifically, you could add that function to the top of your scripts to set the root include folder. Something like this:
set_include_path("/var/www/html/root");
require_once("new/index.php");
You could simply change the included HTML document base by adding a base tag.
https://developer.mozilla.org/fr/docs/Web/HTML/Element/base
<head>
<base href="your_new_url_base" target="_blank">
</head>
The HTML included file could check "Am I included from a parent?" if so "I have to change my base tag"...
// new/index.php
echo '<head>';
if (basename(__FILE__) == basename($_SERVER["SCRIPT_FILENAME"])) {
// called directly, no need base tag;
} else {
// included/required
echo '<base href="your_new_url_base" target="_blank">';
}
echo '</head>';

Find and Append String Regex Php

I'm very new to php, but this problem may require regex manipulation. Basically all "" in my website points to localhost (i.e. img src="http://localhost/folder/img.png" ).
When viewed under source, the "localhost" doesn't show up, but if clicked to get image address, it'll show "localhost". What i need is then to add the full path to that img src (i.e. img src="http://mywebsite.com/folder/img.png" ).
How do i go about doing this?
Any help would be greatly appreciated.
Thanks a lot
Use absolute URLs in your project to reduce conflicts and messy overheads. To use absolute URLs, just define a global constant BASE_URL in your common file like:
define("BASE_URL", "http://localhost/project_folder", true);
Now use it everywhere, like:
<img src="<?php echo BASE_URL.'/img/image.png' ?>" />
In source code, generated html will look like: <img src="http://localhost/project_folder/img/image.png" />
Similary you can use this like:
<script type="text/javascript" src="<?php echo BASE_URL ?>/js/my_script.js"></script>

php: setting root directory for project?

i'm having the following url for my test project on my local server:
http://localhost/projects/test/
now i'd like to have to have the possibilty of using this as root directory for eg. includes/images - like <img src='/img/test.jpg'> - this way it would save me a lot of time as i could simple put it online without any path modifications/flag.
any ideas how this could work?
thanks
I guess this is not a PHP related question, but more on HTML. You may look on the <base> Tag. So instead of saying:
<img src='/img/test.jpg'>
go and make:
<head><base href="http://localhost/projects/test/" /> ... </head>
<body>
<img src="img/test.jpg" />
</body>
Which will in fact point to: http://localhost/projects/test/img/test.jpg
and for the PHP scripts use the set_include_path() function
<?php
$path = '/usr/lib/pear';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
?>
I'm not sure what you are asking, but any image links that do not begin with a forward slash / will be local to the document's path.
So for the document http://localhost/projects/test/test.html
The tag <img src='img/test.jpg'> will point tohttp://localhost/projects/test/img/test.jpg`
If you're using relative paths and assuming that /img/ is a subdirectory of /test/, no change is necessary.
If you want to use absolute paths, you can define a constant somewhere(config.php maybe) with the website root and then reference things like this:
<?php echo "<img src = '" . $root . "/img/test.jpg'>"; ?>
If you're including something you can try to use set_include_path and include_path
http://www.php.net/manual/en/ini.core.php#ini.include-path

PHP template system, image paths

I'm making a basic templating system in PHP, the problem is:
I have a main request handler where from where everything is loaded and processed, but because of that the paths inside the CSS template I downloaded aren't correct anymore.
Example :
<img src="assets/images/contact.gif" />
has to be:
<img src="templates/grey-box/assets/images/contact.gif" />
Is there any way to fix this, PHP wise?
Don't be lazy
Always use an absolute path.
Make it explicit, not with some dirty hacks. It will be a support nightmare.
You can use some helper variables, like $tpl_assets_path and use it in the template.
Absolute path starts from / where / designates web-seite root.
And every path on your HTML page should be.
So, the template should be either
<img src="/templates/grey-box/assets/images/contact.gif" />
or
<img src="<?=$tpl_assets_path?>images/contact.gif" />
define('HTTP_SERVER', 'http://www.myhomeurl.com/');
<img src="<?php echo HTTP_SERVER?>assets/images/contact.gif />
it's here
$imgarray = array('assets','script','css');
$tpl = '/templates/grey-box/';
foreach($imgarray as $i){
$tpldir = $tpl.$i;
$str = preg_replace("/([^\/])($i)([\/])/i","\$1$tpldir\$3",$str);
}

Absolute paths for PHP include

Ok I'm at my work this friday setting up a table that will have data in it that will come from a separate file called values.php. When I write it like this, the divs turn up blank. I think I have the "include" written wrong, does an absolute path not work?
<?php include('http://www.nextadvisor.com/credit_report_services/values.php'); ?>
<div class="box_text">
<div class="box_image">
<img src="<?php echo $offer1logo ?>" alt="<?php echo $offer1name ?>"></div>
<div class="box_rating">
Rating:<span class="star_rating"><img src="<?php echo $offer1star1 ?>" alt=""></span><br>
<div class="rating_review">Go to Site | Review</div> </div>
<br style="clear: both;">
</div>
Oh and thanks in advance for any help you can give. Anyone who has helps me is the greatest. I feel bad cause sometimes two people answer the question and I can't give the green check to both.
Since you're using a URL, PHP will send a request to the server for the file, and the server will parse it just as it would if a browser requested it. What you probably want to do is use a path relative to the script for your include. If the file you want to include is in the same folder, just use
include('values.php');
The type of include you are trying to do is blocked by default, not to say that it is dangerous. You are trying to include a remote file.
If your script is in the same machine as the "values.php" you should use an absolute path similar to "/var/www/nextadvisor.com/credit_report_services/values.php" or a relative path depending on the location of your file, instead of http://www.nextadvisor.com/credit_report_services/values.php
It doesn't seem to me that you are trying to include a remote file but if you still want to include a remote file you must set the following directives in your php.ini
allow_url_fopen = 1
allow_url_include = 1
Absolute filename is something like /path/to/file.php as you would access the file from anywhere in the file system hierarchy.
If its in the same dir, or relative to the same dir, you can use something like
include dirname(__FILE__) . '/file.php';
include realpath(dirname(__FILE__) . '/../dir') . '/file.php';
include realpath(dirname(__FILE__) . '/../dir/file.php');
You should probably make some constants like
define('BASEDIR', dirname(__FILE__));
define('LIBDIR', BASEDIR . '/lib');

Categories