I have created a php file that consists path say rootdir/level1/level2/sample.jpg Now i want to include this file from any where i.e., from any level. how to do it. So that i can get image in any level of path.
Try this.
<img src="<?php echo $_SERVER['SERVER_NAME'];?>/level1/level2/sample.jpg">
Related
I tried to use a URL in the parent directory of the root but it's not working in the HTML attribute of the PHP file.
<?php
include ("../func.php"); // working, will include the file in the parent directory
?>
<img src='../testimg.png'/> <!-- It only looks inside the current directory-->
<img src='..../../testimg.png'/> <!-- If I mess around with the relative path, it still looks inside the current directory-->
However, if I delete the PHP code and change the file extension to .html, the relative path in img src works normally.
Why is this happening and how could I solve the problem? I am wondering is there any PHP configuration file I need to adjust?
I have been stuck with this problem for a long time and googled a lot but didn't find a working solution. I would be appreciated for any help.
I tried your code in my local with the following directory structure
root_folder
|---my_folder
|__index.php -> (this file contained your code above)
|---func.php
|---testimg.png
<img src='../testimg.png'/> showed my image perfectly.
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.
Before going on, I'd just like to say that I've tried pretty much everything, and I read a lot of questions here at Stackoverflow, but none of the solutions worked.
So here is the deal, I have a file ("empresaConf.php") that contains this code:
<?php
include 'navigator2.php';
?>
<div class="contTudo">
<div id="bgCorpo">
<div class="empresaConf">
<?php
include 'sqEsqInc.php'
?>
</div>
</div>
<?php
include '../rodape.php';
?>
</div>
</body>
</html>
As you can see, it is including "rodape.php". The problem is that in "rodape.php", I have an image that is in another folder, so when I include "rodape.php" in "empresaConf.php", the image path should change, since PHP include consider the file path to be the one that you're including other files, making rodape.php's image broken when I access "empresaConf". I want something to fix that so I have the image no matter what file I'm in. Here is the "rodape.php" code:
<div class="empresa">
/* below in the img src tag you can see what I'm talking about */
<div class="redeSocial">
<a href="http://www.facebook.com.br">
<img src="imgs/icons/iconFace42.png" class="iconeFace">
</a>
</div>
</div> <!-- Fim div .empresa -->
The folder structure is: "rodape.php" is in the root directory and "empresaConf.php" is inside a folder named "Empresa" (I would post a img but I can't).
Thanks in advance.
EDIT:
Sorry, I forgot a crucial detail, I'm also including "rodape.php" in other files such as "index.php". So if I change the img src to correspond "empresaConf.php", then in the "index.php" the image will be broken, since it's in the root directory, same as "rodape.php".
If it's an image accessible from the public side of things, the image can probably be accessed by the directory root. So if your index.php is in /index.php where / is the web document root and your image is in /imgs/icons/iconFace42.png you can access the image by
<img src="/imgs/icons/iconFace42.png" class="iconeFace">
Note the leading slash in the src attribute.
You have some options.
If rodape.php will only be accesed from that folder level, simply change the src to ../imgs/icons/iconFace42.png, if you will be changing folder deep then use an absolute path to the image. Another way would be add php code to set the src but that would be wast of resources.
When you include ../rodape.php, think of it like just putting the code from that file directly in the position where it was included. The path string will not change, as it has not been executed until it has been included.
In short, that is to say, things will be relative to the file that's being executed, not the file(s) that's being included.
<img src="../imgs/icons/iconFace42.png" class="iconeFace"
Using that image code should resolve your issues.
This seems to have been asked around, but with different answers.
I've the image folder "images" in the same directory as the page.php file which I want to add images to. Yet the path "images/foldername/image.gif" doesn't yield any images on the page. Whereas from a non-wordpress html file that path would work, so what is the answer to this? What makes wordpress not understand file paths, assuming I'm not missing something?
also tried /images with the slash just in case.
You may use:
echo get_bloginfo('template_url') . 'images/imagename.png';
Or
<img src="<?php bloginfo('template_url'); ?>/images/img.png" />
bloginfo, get bloginfo. Also, read this article.
You mean inside the WordPress themes folder? If so, correct path is http://www.domain.com/subdirectory/wp-content/themes/yourtheme_name/images
You can use <?php get_template_directory_uri(); ?>/images/foldername/image.gif
get_template_directory_uri()
I have a php file, for example www.example.com/folder/test.php
The file above (test.php) has the line
include($_SERVER['DOCUMENT_ROOT'].$page);
$page = /folder2/.$directory1./.$directory2./.index.php
$directory1 and $directory2 are all random.
However,
The index page has an image on it that just references the image as the source.
For Example src="image.bmp"
So what happens is the image's url actually shows up like this...
www.example.com/folder/image.bmp
And it should show up like this...
www.example.com/folder2/.$directory1./.$directory2./image.bmp
Is there a way to fix this besides using absolute paths?
There are millions of directories and we would like to correct the issue rather than correct the problem (even though we will do both).
Can I place an htaccess file in the directory its looking for the image in to redirect it?
FROM: www.example.com/folder/
TO: www.example.com/folder2/.$directory1./.$directory2./
If you want to do that, you should either dynamically change the .htaccess or specify in it any possible $directory1/$directory2 combination. In my opinion, the best way to do this is to change the src of that image in the file index.php to something like:
<img src="/folder2/<?php echo $directory1; ?>/<?php echo $directory2; ?>" />