php: setting root directory for project? - php

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

Related

How can I use relative path in CodeIgniter?

I know a little bit about relative path. Suppose, I have a project on codeigniter. There are 4 controller such as with function name:
1. Admin->index,profile,add_student,edit_marks,make_admin,
2. Teacher->index,add_marks,update_marks
3. Student->index,view_teacher,view_result
4. Guest->index,view_Student, view_marks,view_result
From these, How can I use them as relative path ?Remind it,some times I go one controller to another controller, such as profile,view_marks is a common function for all, so every one access it.
For images or file can I use relative path ? Then,Suppose I have a image at root folder as
->root_folder/template/images/a.png. How can I use it as relative path ?
Really, I need the core knowledge of relative path with application in codeigniter.
You should use codeigniter constants defined in root/index.php
To get Root path use
FCPATH
It returns
Path upto root folder where your codeigniter is installed.
So in your case , it will be
FCPATH . 'template/images/a.png';
I assume you're looking for path not URL.
At last I have found the answer that is:
It is not necessary to use base_url or site_url every times for link.
All time use:
/controller_name/function_name
Such as for a anchor link on viewing admins's profile code as(I hope, this technique is called relative path). It decrease time to route it.
Admin Profile
What's wrong with using this for the image path?
base_url('path/to/image')
If having senario like
ROOT
|____APPLICATION
|____ASSETS
| |________ CSS
| |________ JS
| |________ IMG
|____SYSTEM
then you can access the css as follows:-
<script type="text/javascript" src="<?php echo base_url();?>assets/css/style.css"></script>
<script type="text/javascript" src="<?php echo base_url();?>assets/js/getinfo.css"></script>
<script type="text/javascript" src="<?php echo base_url();?>assets/img/image.png"></script>
The current accepted answer works fine, but it is not relative. For instance: if the website would be replaced from the root to a subdirectory, the url wouldn't work anymore.
More on this same topic I found here: How can I display images with the Codeigniter php framework?
The answer in that topic recommends not working with relative paths, but personally I would like to see this possible within modules (the MX-plugin for CodeIgniter).
So here's a possible way of working with relative paths I found:
<?php $path = base_url(str_replace(FCPATH,"", __DIR__ )).'/'; ?>
Example usage:
<img src="<?php echo $path;?>../assets/img/myImage.png" />
for instance: if the this html would be used in /modules/mymodule/views/someviewfile.php the output will be yourdomain.com/modules/mymodule/assets/img/myImage.png

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>';

Included file's root path

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" />

PHP: Defining relative paths compatible with HTML Tags

Consider the following directory structure:
ROOT
------ images
............... logo.png
------ includes
............... vars.php
------ layout
............... content.php
------ index.php
How do I define a path constant for logo.png in vars.php that is accessible in both index.php and content.php? Should be compatible with HTML Tags as a relative path.
<img src="<?php echo IMAGE_PATH; ?>">
which should be parsed as
<img src="images/logo.png"> <!-- if used in index.php -->
and
<img src="../images/logo.png"> <!-- if used in content.php -->
New Question (EDIT): Does root-relative path work when including php files using include / require methods?
Try setting the <base> tag in the <head> section of your code.
All your images, css, and js files will use this instead of the url in the address bar.
Info on base
Absolute url or root paths will give you the least amount of headaces. Trust me, when the system grows you'll regret that setup.
It is a perfectly legal way to reference things. (as you ask in the comments)
If you're worried about setups between domains, just create a config variable with the absolute path to the domain / directory / etc
You can use "root-relative" paths. Simply link to everything with a forward slash at the beginning, i.e.
<img src="/images/logo.png">
This will resolve to http://yoursite.com/images/logo.png from every page on yoursite.com.
simply specify all paths as relative to the root
<img src="/images/logo.png"> <!-- will work anywhere -->
I'd suggest, primarily, that you use root-relative paths. This is only to reduce the complications of moving your site to another host, and also it allows for consistent paths (rather than using an if() condition to test from where the script's being run).
But otherwise, your suggestion would be fine.
I would use something like an application base URL:
define('APP_URL', 'http://example.com/path/to/app');
echo '<img src="'.APP_URL.IMAGE_PATH.'">';
Or to have it more convenient, write a function that resolves your relative URL to an absolute URL.

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