How to go up a directory with ajaxpages? - php

The problem I got, is that I can't go up a directory to open an ajaxpage.
Here is the link to the directory above:
<img src="../images/buttons/community.png" alt="Community" />
This won't work. What have I done wrong?

Javascript paths are relative to the page being displayed. Assuming you're viewing a page in the pages directory, you're actually requesting pages/pages/community.php.
Use community.php as your URL or use an absolute path.

Related

php - link to root directory not working

This is the hierarchy of my folder and files:
/website
/admin
/about
editAbout.php
adminHeader.php
adminDashboard.php
adminLogout.php
adminHeader.php is the header of my pages, so it is being included in all pages. It also contains the Logout link that has this code:
Logout
In adminDashboard.php page it works, but in editAbout.php page, it didn't work because the link is not right, it becomes
http://www.domainname.com/admin/about/adminLogout.php
I tried to change the link to
<a href="http://www.domainname.com/admin/adminLogout.php">
and also by using the $_SERVER['DOCUMENT_ROOT'] but didn't work also.
Does anyone know how to reset the link to the right one?
you can use ../ to come back to the parent folder
it becomes enter code hereLogout
Try this:
$_SERVER['SERVER_NAME'] . "/admin/adminLogout.php";
The SERVER_NAME value refers to the domain.
You do not need to add the server name, as the browser will put that in for you. So just make all of your paths absolute (from the web browser's point of view):
/admin/adminLogout.php
Example:
<a href="/admin/adminLogout.php">

Missing CSS file and images after URL rewrite

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.

wordpress image path changing dependent on file

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

mod_rewrite changes the current dir

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.

Change the default path where the web server looks for images

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

Categories