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; ?>" />
Related
I am facing some typical issue in loading image files in PHP. Image got successfully loaded but cannot get displayed properly.
I have declared a define value, then echoed it in an img tag src attribute :
define('image_site_url','localhost/projects/faantush/product_images/');
<img src="<?php echo image_site_url.$row['image1'] ?>" />
The image file is not found but it is there in product_images. Here are the pictures:
In the HTML code: You are missing the: http:// before localhost
<img src="http://localhost/ ...
If your pictures are hosted on the same server than your website, you don't really need to use an absolute path. You could use a relative path and remove the domain name (localhost in this case):
<img src="/projects/path/to/your/picture.jpg" />
You must use absolute paths when linking to another website, but you can also use absolute paths within your own website. This practice is generally frowned upon, though. Relative links make it easy to do things like change your domain name without having to go through all your HTML pages, hunting down links and changing the names. As an added bonus, they force you to keep your site structure neat and organized, which is always a good idea.
An interesting use case of not using absolute paths is, if you want to set your website to SSL/TLS: you will have to change all the occurrences of http to https. Although it is not a big deal, this is generally not the kind of work you want to do.
See Relative path or url for html src and href attributes and the post from where this quote is taken.
I am attempting to integrate a Wordpress blog page into my (php) website. So far I have been successful in creating an installing it into a subdirectory. I can see that this works.
The issue I am facing is that images are not showing (the alt text is). The issue is that Wordpress uses a incorrect path to the flies:
Wrong Path: https://sitename.com/wordpress/blog/images/brand/logo.png
Correct: https://sitename.com/images/brand/logo.png
It should not have /wordpress/blog/in it.
How do I go about resolving this to point at the correct directory without duplicating the files. Note that in the code the path is is written as: /images/brand/logo.png
If your image folder lays inside theme directory, then you can use
<img src="<?php bloginfo('template_url'); ?>/images/brand/logo.png" />
If your image folder lays outside the theme directory, then you may use
<img src="<?php echo home_url(); ?>/images/brand/logo.png" />
Put this in your htaccess in your root directory.
RewriteEngine On
RewriteRule ^wordpress/blog/images/(.*)$ /images/$1 [L]
That should rewrite those urls.
Depending on your needs, you may also want to consider this article. It doesn't sound like your problem is that you want to change all urls, only the urls to your images.
If you already have path as /images/brand/logo.png , just go 2 levels up like this;
../../images/brand/logo.png
If you are using a wordpress default theme/img upload folder then wordpress build in function get_stylesheet_directory() would work for you. In this case no matter where you shift your project it will always grab this path, this much better then hardcoding path every time.
I'm having problems with the paths to my first wordpress theme. Post images and stuff not related to css is located in wordpress_folder/blog-images/ and in index.php when I link to images I use this path: blog-images/img.jpg
The problem is now that when I want to link to the same image from another file (not index.php) in this case single.php wich displays one blog post, the correct path is now ../../../blog-images/img.jpg
This is causing problems in the includes like sidebar etc. sidebar.php works fine when called from index.php but the images path is changed if sidebar.php is called from single.php.
Does anyone know what's going on?
If you are creating these links from within php scripts, I would suggest using the site_url() function to get the URL for your wordpress install and then appending your images path to the end of that. If you are editing static theme files like css, then you should use /wordpress_folder/blog_images/img.jpg.
Something like <img src="<?php echo site_url() ?>/blog_images/img.jpg" /> should be sufficient from theme files.
The reason that paths are chaning is because if you are in wordpress_folder then the path blog_images/img.jpg resolves to wordpress_folder/blog_images/img.jpg but if you are on a post that has the url yoursite.com/wordpress_folder/2011/09/category/my_great_post then the path would resolve to wordpress_folder/2011/09/category/blog_images/img.jpg which is obviously incorrect.
For this reason you should try to use the absolute path or full URL so that no matter what file/folder/url you are linking from, the path will always be correct.
The main downside you may run into is that if you were to change the name of your wordpress folder, or remove it altogether, then you may need to make a lot of edits to reflect that. But in any case, you should put the / in front of your path so that it can be referenced the same from everywhere.
Also check out the site_url() reference page, it lists some other helpful functions at the bottom that may be useful to you.
I thought this was a little unclear from drew's answer, so I am adding a little bit more in a separate answer. His advice is sound and I agree with him.
If you prepend a url with a / then it will navigate based on your site url. Without the slash it uses relative navigation.
So here are some examples for www.mydomain.com
//always shows the image located at http://www.mydomain.com/myfolder/pic.png
//no matter what the url is
<img src="/myfolder/pic.png" />
//shows the image located relative to the current path
//if current url is http://www.mydomain.com/posts/ then the image will come from
//http://www.mydomain.com/posts/myfolder/pic.png
<img src="myfolder/pic.png" />
If you are creating links dynamically from php side then you will want to use site_url().
If you are creating links to your theme directory folder then you will want to use bloginfo('template_directory')
I have a php file that loads an article from a db based on the given variables. There is also an .htacces file in the root of the site. I used this in the htaccess to redirect
RewriteRule
^articles/([a-zA-Z0-9-_\s]+).html$
template/index.php?action=viewarticle&alias=$1
after redirecting, the page shows fine but the html in the page goes wrong, for example:
media/2011/02/21/logos.jpg turns in to articles/media/2011/02/21/logos.jpg
This happens because the htacces is redirecting. Is there anyway to do this redirect while keeping the root dir unchanged?
This happens coz the htacces is redirecting.
No, this happens because the browser thinks that
example.com/articles/my_article.html
is a resource in the /articles sub-directory, and treats all relative URLs as relative to /articles.
There is no way to change that behaviour.
You will need to start using absolute image references, or relative image references that consider the additional directory:
<img src="/media/2011/02/21/logos.jpg"> <------ recommended
<img src="../media/2011/02/21/logos.jpg">
you could also use <base> as suggested by #Boris but absolute paths (or full URLs) are a vastly cleaner solution to the problem in my opinion.
First, what do you mean by "html in the pages goes wrong":
Is it the link showed in the status bar?
Is it the actual href? If it is, you probably use some view helper which construct your "base url"
Maybe you "link" your resource without specifing an absolute path (using /), then your resource are "relatively" linked to current page (/articles/)
.htaccess don't change anything in your code.
There is an html element which allow you to define base url used everywhere in your page.
<base href="/root" />
if you have for example Article 12 then when clicking on the link, you will redirect to /root/articles/12
Also, mixing Pekka's answer with Boris', you should define somewhere in your application which is the root path of your application and output all paths as absolute, prepending the base dir you defined earlier.
for example: in config.inc.php
define("ROOT_URI", "http://myserver.com/myapp");
everywhere:
<img src="<?php echo ROOT_URI;?>/media/2011/02/21/logos.jpg
This is like using the base element as Boris suggested, without using it (I also dislike base), and makes your application able to work in whatever folder under the webserver it is stored.
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