更改 Web 服务器查找图像的默认路径

发布于 2024-09-29 05:04:28 字数 578 浏览 0 评论 0原文

我正在尝试更改默认路径或添加网络服务器查找图像的路径。我真的很想要一个在 PHP 中而不是在 htaccess 中执行此操作的解决方案。

最基本的例子是试图“破坏”当前的实现,所以说我有一个包含以下内容的目录:

ma​​in/

  • 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?!?! -->

如果您理解我的观点或者有任何疑问,请告诉我。

谢谢大家! 马特·穆勒

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浮光之海 2024-10-06 05:04:28

我认为您混淆了服务器文件系统上的当前工作目录和 Web 服务器文档根目录。

当您在 HTML 中创建图像元素时,它(浏览器)会根据一些参数查找源。

  1. 如果 src 路径是相对路径(没有前导斜杠),则图像将相对于 元素 URL(如果已设置)加载,否则为当前 URI
  2. 如果 src 路径是绝对路径,则将加载图像从文档根的绝对路径,例如 将从 http://example.com/foo/bar/baz.jpg
  3. 如果 src 是绝对 URI,那么它将简单地从该 URI 加载

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.

  1. 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
  2. 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
  3. If the src is an absolute URI, then it will simply load from that
一曲琵琶半遮面シ 2024-10-06 05:04:28

img 标签被发送到客户端
更改预处理器的目录不会更改客户端的目录,因为该目录固定为它们所在的当前页面,例如 http:// /example.com/

您需要更改每个 img 标签的 src 来更改要查看的目录。

为了避免将来的混乱,您可以使用一个为正确目录添加前缀的函数。

例如

<img src = "<?php echo produceImageURL('image.png'); ?>" width = "402" height = "265" alt = "1" />

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" />
吲‖鸣 2024-10-06 05:04:28

什么是PHP的相对路径和页面的相对路径是两个不同的东西。

您更改了当前 PHP 脚本的目录。但是,请求的页面及其资源仍然相对于 main/index.php

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

心碎的声音 2024-10-06 05:04:28

jnpcl 说得对:

<base href="../../../">

把它放在你的 之间。 通常在 之后,加载任何文件之前

jnpcl had it right:

<base href="../../../">

put that between your <head> </head> typically after <meta />, before you load any files

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文