从文件中IMG,一个和头相对路径 [英] Relative paths from file for img, a and header

查看:233
本文介绍了从文件中IMG,一个和头相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个也可以访问域/类别或域/分类/条一个动态的网站,并重定向到域/的index.php?类别/条。

I have a dynamic website that can be accessed as domain/categories or domain/categories/article, and it redirects to domain/index.php?category/article.

我现在面临的问题是,我想使用相对路径的文件。我成功地做到这一点对包括与目录名(文件),如

The problem I'm facing is that I want to use relative paths from the files. I managed to do that for includes with dirname(FILE), as in

ini_set('include_path',  dirname(__FILE__) . '/include');

这工作得很好。  根据不同的目录,我将其修改为,它也适用

This works fine. Depending on the directory I will change it to, and it also works

ini_set('include_path', dirname(__FILE__) . '/../include'); (extra /../)

但是,如果我试图做同样的IMG SRC,A HREF或php头,而我得到了正确的文件路径,图片或链接,他们根本不工作。什么也没有发生,如果我没有点击的。     点击一个链接,通过PHP和生成的:

But if I try to do the same with img src, A href or php Header, while I get the correct file path to the images or links, they simply do not work. Nothing happens as if I didn't click at all. Clicking on a link generated through php with:

print "<a href='" . dirname(__FILE__)  . strtolower(str_replace(" ", "", $row['pageSection'])) . "/" . strtolower(str_replace("", "-", $row['pageTitle'])) . "' alt=''  >";     

不工作,即使在HTML源代码code在读为:

Does not work, even if the html source code is reading as:

 <a href="C:\validpath\htdocs\domain/category/article>

我怎样才能使路径IMG,链接(和PHP头),相对于文件的位置,并没有网址,并使其发挥作用?

How can I make the path to the img, link (and php header) relative to the file location, and no the url, and make it work?

边注:即使我的CSS刹车,如果我接取我的网站通过域名/ prettyurl /,但如果我通过域/ prettyurl(无结尾斜杠),pretty的怪异访问

Side note: even my CSS brakes if I acess my site through domain/prettyurl/, but not if I access through domain/prettyurl (no ending slash), pretty weird.

推荐答案

得到它的工作本地主机上和网上,这里是我的设置这样:

Got it working on local host AND online, here is my setup so:

1)你可以使用它你自己  2)给出一个更好的选择给我们新手

1) You may use it yourself 2) Give a better alternative to us newbies

我用什么zoranc上面说的。例如,让CSS工作的dynamic- pretty的-URL的网页上,我将其设置为:

I used what zoranc said above. For example, to get CSS to work on the dynamic-pretty-url pages, I set it up as:

<?php
print "<link href='http://" . $_SERVER['HTTP_HOST'] .'/' . dirname($_SERVER['PHP_SELF']) . "/CSS/main.css' rel='stylesheet' type='text/css' />";

?>

警告:code以上会产生如Domain /// CSS链接。我还需要过滤额外的斜线出来,但它的工作原理,即使它是CSS和其他一切(IMG,一,PHP头)

WARNING: the code above will produce links such as Domain///CSS. I still need to filter the extra slashes out, but it works even as it is for the CSS and everything else (img, a, php headers)

然后,我不得不这样做每次我想:

Then I had to do this everytime I wanted to:

  1. 创建一个动态链接
  2. 创建一个动态的IMG
  3. 即使我的头形!由于我的头总是包含在内,当你在浏览pretty的-的URL,表单不能有行动='的header.php',因为它会尝试找到它不管pretty的-URL你当前浏览(域/分类/文章等)。你必须使用头='PHP我们讨论/ header.php文件endphp的解决方案。

  1. Create a dynamic link
  2. Create a dynamic img
  3. Even for my header form! Since my header is always included, when you are browsing pretty-urls, your form can't have action='header.php', because it will then try to find it on whatever pretty-url you are currently browsing (domain/category/article and so on). You'll have to use header='" php the solution we are discussion/header.php endphp.

再次被警告这将产生额外的斜线,你就必须以某种方式将其删除。

Again, be warned this produces extra slashes and you'll have to remove them somehow.

通过这种设置,你可以有一个网站上的本地主机和所有虚拟主机的作品,而不需要改​​变code,但它是有点繁琐,容易出错的使用。如果任何人有一个更好的解决方案,请分享一下。

With this setup you can have a site that works on your localhost and any web host without the need to change code, but it is somewhat cumbersome and error prone to use. If anyone has a better solution please share.

此作品不够好,在我看来绝对是不必改变code,当您上传您的网站,每次,这更加容易出错,我想更好的。

This works well enough and in my opinion is definitely better than having to change code when you upload your site everytime, which is even more error prone I suppose.

再次感谢Zoranc,真正有用的。

Thanks again Zoranc, really helpful.

这篇关于从文件中IMG,一个和头相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆