I'm trying to change the default path or add a path that the webserver looks for images. I really would really like a solution to do this in PHP and not in htaccess.
The most basic example would be trying to "break" the current implementation so say I have a directory with the following:
main/
image.png
index.php
In index.php:
<?php
// Change the directory WAY out of current directory
chdir('../../../');
echo getcwd(); // DEFINITELY NOT where image.png is located
?>
<img src="image.png" width="402" height="265" alt="1">
<!-- WHY ARE YOU STILL RENDERING?!?! -->
Let me know if you understand my point or if you have any questions.
Thanks all!
Matt Mueller
I think you're confusing the current working directory on the server filesystem and the web server document root.
When you create an image element in HTML, it (the browser) looks for the source based on a few parameters.
If the src path is relative (no leading slash), the image will load relative to the <base> element URL if set, otherwise the current URI
If the src path is absolute, the image will load from the absolute path from the document root, eg <img src="/foo/bar/baz.jpg"> will load from http://example.com/foo/bar/baz.jpg
If the src is an absolute URI, then it will simply load from that
The img tag is sent to the client.
changing the directory of the preprocessor will not change the client's directory, as that is fixed to the current page they are on, such as http://example.com/.
You would need to change each img tag's src to change the directory to look in.
To avoid future confusion, you could have a function that prefixes the correct directory.
e.g.
<img src = "<?php echo produceImageURL('image.png'); ?>" width = "402" height = "265" alt = "1" />
What is the relative path for PHP and the relative path for the page are two separate things.
You changed the directory for the current PHP script. However, the requested page and it's resources are still relative to main/index.php
jnpcl had it right:
<base href="../../../">
put that between your <head> </head> typically after <meta />, before you load any files
Related
I am a newbie in website development.
here is my problem :
I have 2 HTML files. they are 'index.php' and 'header.php' . I try to include 'header.php' in to the 'index.php' using this code :
'index.php'
<body>
<?php
include("header/header.php");
?>
</body>
'header.php' contain this code :
<h1>Its header</h1>
<img src="img/006-tumblr.png" width="200" height="200">
its the folder hirearchy :
-index.php
--header
--img
-006-tumblr.png
-header.php
When I open 'index.php' , 'header.php' is included but the image is not displaying.
So how can I include 'header.php' with the image?
A good way of proceeding would be to have a folder "img" in your root public HTML path containing all images, eventually with subfolders to separate them. The reason for that is that your main controller is launching from root. In that way, wherever you call your image file, you just have to go to img/ in order to find it. The same logic applies to all media. So, if you intend to have video for example, you could have a main folder media with a img subfolder and a vid subfolder. This type of logic has to be defined at start of project so that you don't have to refactor in the middle of it.
The logic of including a file into another is different from linking a CSS file. When you include a PHP file, the entire code is added to the source file and then the server compiles the codes. So the image files and other resources should be addressed relative to the source document (not the included one). This logic is different in a CSS file and the resource files e.g. a background image is complied relative to the CSS file (Because a CSS may be used in different files hirearchy). So this will work:
<h1>Its header</h1>
<img src="header/img/006-tumblr.png" width="200" height="200"
Footnote: If you want to use header in different files with different hirearchy the solution for the question above is to set BASEURL for your document and setting the resource and anchores relative to the baseurl.
Giving location from root directory makes image accessible from any location.
<?php define('WEBSITE_BASE', $_SERVER["SERVER_NAME"]); ?>
<img src="<?=WEBSITE_BASE?>/header/img/006-tumblr.png" width="200" height="200">
Best linking policy you should always follow to link static assets to your page so that it links wherever the item is used.
My question might not be clear, so here is an example.
I have a PHP script that will auto add the relative path to all of HTML resources like
CSS - <link href href="<?php echo $siteroot ?>css/main.css" ... >
JS - <script src="<?php echo $siteroot ?>js/main.js"</script>
Images <img src="<?php echo $siteroot ?>img/avatar.jpg" ... >
other uses like PHP includes
the script will auto make the relative path to the site root and this will vary to be '../', '../../', '../../../', or an empty string '' if it is the site root folder - main index
My question does this will affect the cache system the browser uses? I thought of this because the same resource will be different in many pages that are in subfolders!
once ../../img/avatar.jpg other ../img/avatar.jpg, other img/avatar.jpg alone!
I have tried with chrome and run a file with img and then deleting the img and opening a file in a subfolder. This worked and the image was there!
I still not sure and want a granted answer about the caching process for relative paths. If there is any extra information, I will appreciate it :)
Thank you
These resources will be cached once because absolute path is same, regardless they relative paths differ. Browsers uses absolute paths for caching.
I have a form handler which is written in PHP and resides in a different directory than the html files. When the handler runs, it needs to include one of the html files. The html files have relative hrefs in them, which break because the page was served from the PHP directory, not the html directory.
For example, index.html contains
<link rel="stylesheet" type="text/css" href="css/site_global.css?4013920463"/>
These links are produced by Adobe Muse and expect that "css" is a subdirectory under the location of the html files and that the page was served from the html directory. Again, since I'm serving the page from the PHP directory, the relative links break.
Short of putting in absolute paths for the hrefs, is there any other technique I should consider? I really don't want to put in absolute paths because they will break for other reasons.
Ideally, I'd like to use some sort of method that allows me to set the "working path" in the browser - so that I can tell it to fetch hrefs from the right place.
Relative paths in a browser are computed based on the current page path (see here). If you are looking at http://foo.bar/one/page.html , the site_global.css path will be http://foo.bar/one/css/site_global.css .
If I understood your question, you can use the element to set a base URL for all the relative links in the page.
See here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
try $_SERVER['DOCUMENT_ROOT'], gives the path to your base directory with current working dir
or
try echo realpath(dirname(FILE));
I'm trying to make user friendly URL using mode rewrite.
My problem is, that after giving category like 'name' to my URL, when I call the page using new URL, it can't load the CSS file or images.
I have a link like:
localhost/mywebsite/project?id=22
New link is something like
localhost/mywebsite/project/22/myproject.project
htaccess code:
RewriteRule ^project/([0-9]*)/.*\.project$ /project.php?project=$1 [L]
(it might not be 100% right but I don't have access to my code right now so I just wrote this and it works fine on the original source)
My root directory is localhost/mywebsite/
and my CSS file is in css/style.css
localhost/mywebsite/css/style.css
my htaccess
localhost/mywebsite/.htaccess
and my project.php file is in
localhost/mywebsite/project.php
So in the project page I have access to CSS file by using relative path,
<link href="css/style.css" rel="stylesheet" type="text/css" />
but when I use rewritten URL page can't find the CSS file.
I can't use absolute path with domain name because I don't have domain yet! and it can be anything.
one way is to use relative path to domain as suggested on the similar questions
localhost/mywebsite/project.php
and when i run my script localy my root directory is
localhost
so css link should look like
href="mywebsite/css/style.css"
but when i go live i should change all links to probably something like
href="/css/style.css"
this seems like lots of work
For your local version add
<base href="//localhost/mywebsite" />
to the head section
and for your live versions change it to
<base href="//your.domain.here" />
reference at http://www.w3.org/TR/html4/struct/links.html#h-12.4
you have to define the base path or the server view path in the connection.php and whenever u want that path, make that global. then that variable will b called and the css or images will take the whole path.
for example
$SVP="http://www.example.com/"
global $SVP;
echo $SVP;
so
Insert an image into the same file with the same relative path as the css href link, load the page in a browser, right-click the image in internet explorer, click properties and you should see where the relative path actually points to.
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.