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');
Related
I'm stuck in a simple thing as loading an image in php.
All included php files are in a directory called includes.
Index.php is in root folder.
When I call img src from the php file located in the root folder, I can see images.
But when I include a section that is supposed to load an image like that:
<?php include("includes/section.php"); ?>
calling this:
<div class = "right">
<h1> news </h1>
<hr>
echo '<img src="../img/do.png" width="200" alt="Rifrazione news/>';
<p class = "rightp">Blabla </p><br
</div><!--end of right-->
I get nothing
for images I prefare use global variable.
Try do something like this:
<?
$baseUrl = "http://localhost"; // CHANGE IT TO YOUR URL
?>
<div class = "right">
<h1> news </h1>
<hr>
<?
echo '<img src="'.$baseUrl.'/img/do.png" width="200" alt="Rifrazione news" />';
?>
<p class = "rightp">Blabla </p><br />
</div><!--end of right-->
The file you're currently in is your current working directory. So find the location of your image from the directory your index.php is in, that means probably removing the ../. If you have any doubts echo getcwd();.
NEVER do what Adam suggested. You should always use file system paths, never url paths to local files. EDIT Reasoning: Hotlinking is part of it as he may not be aware of why something isn't working as it has worked in other locations but mostly due to routing/.htaccess/url rewriting. If he were to move on later to an MVC or similar framework putting localhost/controller/action will not return his image, but he may not be sure why as this is a bad habit he has already picked up. The other reason was for ease-of-use, if he puts a path like ./image/here.jpg opposed to localhost*/here.jpg he may have to go through and rewrite every link if he were to migrate later to an external server.
Example to help visualize better:
If you have the following directory:
my_site/
js/
css/
images/
image.jpg
index.php
bob/
bob.php
contactBob.php
If you were viewing index.php, your link to images would be /images/image.jpg.
If you were viewing contactBob.php your link to images would be ../images/image.jpg.
In HTML5, you can declare the <base> as a DOM element.
After searching and testing...it was none of that.
The only thing missing was the php tag at the beginning of the bracket.
You don't need variables at all.
This syntax is working as intended.
<?php
echo '<img src="img/do.png" width="120" alt="Rifrazione news"/>';
?>
Thanks for jumping in, anyway.
I would like to include a header in every page using PHP, so that when I edit the header I won't have to change it on every page. However I am pretty sure if I do that, the relative links will do it according to the page that the PHP is included in. Is there any problem using absolute links to do this? Sorry if this is a simple question and just wondering if there are any rules related to this.
<header>
<div class="navbar">
<img style="margin:10px;" src="../images/logo.png" alt="logo"/>
<img style="position:relative;bottom:14px;" src="../images/line.png" alt="line"/>
<img class="navbaricon" id="one" src="../images/home.png" alt="home"/>
<img class="navbaricon" id="two" src="../images/artist.png" alt="artists"/>
<img class="navbaricon" id="three" src="../images/releases.png" alt="releases"/>
<img class="navbaricon" id="four" src="../images/join.png" alt="join"/>
</div>
<div class="artists">
<div class="artistlinks">
<p>Brady Hartvigsen</p>
<img src="../images/artists/bradymain.jpg" alt="Brady Hartvigsen"/>
<p>Catalyst</p>
<img src="../images/artists/catalystmain.jpg" alt="Catalyst"/>
<p>Emmi Moffitt</p>
<img src="../images/artists/emmimain.jpg" alt="Emmi Moffitt"/>
<p>Frederik Jyll</p>
<img src="../images/artists/fredmain.jpg" alt="Frederik Jyll"/>
<p>J.R. Hansen</p>
<img src="../images/artists/jrmain.jpg" alt="JR Hansen"/>
<p>Kate Berry</p>
<img src="../images/artists/katemain.jpg" alt="Kate Berry"/>
<p>Ryan Cluff</p>
<img src="../images/artists/ryanmain.jpg" alt="Ryan Cluff"/>
<p>Silter</p>
<img src="../images/artists/siltermain.jpg" alt="Silter"/>
</div>
</div>
<script>
$("a:nth-child(4)").click(function () {
$(".artists").animate({width:'toggle'},500);
});
</script>
</header>
This is what my header currently looks like
You can use both absolute and relative paths. When using relative paths you have to make sure the path is relative from the file you are including from.
I would recommend to use relative paths, since moving the site to another server or path on the server would be much easier.
Also, you may be talking about relative html links. I solve that by setting a "rel" parameter in the main file.
Both types of relative solved in this sample:
/mysite/webroot/index.php
<?php
define('RELPATH','');
include_once(RELPATH.'../includes/header.inc.php');
...
?>
/mysite/webroot/otherpage/index.php
<?php
define('RELPATH','../');
include_once(RELPATH.'../includes/header.inc.php');
...
?>
/mysite/includes/header.inc.php
<html>
....
<body>
..
Front page<br />
Other page<br />
I believe you're referring to including a PHP file in another.
Is there a rule? No. But it is always best to stick to relative paths. Pay attention to the following:
If you use absolute paths to folders/sub-folders you'll have trouble when the site is moved to a different location (even within the same domain). So stick to relative references.
Have some sort of 'escape route' so that if someone tries to read an include file directly it should not make the system vulnerable. For example, you may have a variable set to a certain value and check for it in the file. This is not necessarily a security thing but it may prevent the site from 'behaving in an unexpected manner'.
Example of including a php file in another php file...
Steps:
1. Create a separate file (eg: myheader.php). Remember, this must be PHP. Not HTML.
2. Paste your header in it. Save.
3. In the file that needs to call it, include this line or something similar:
<?php include "myheader.php">
if the file is in a folder above the current folder, use something like this:
<?php include "../myheader.php">
To go two levels above the current folder, use this:
<?php include "../../myheader.php">
If the file is in a sub folder within the current folder, use this:
<?php include "SubFolderName/myheader.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" />
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
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.