更改 Web 服务器查找图像的默认路径
我正在尝试更改默认路径或添加网络服务器查找图像的路径。我真的很想要一个在 PHP 中而不是在 htaccess 中执行此操作的解决方案。
最基本的例子是试图“破坏”当前的实现,所以说我有一个包含以下内容的目录:
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?!?! -->
如果您理解我的观点或者有任何疑问,请告诉我。
谢谢大家! 马特·穆勒
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您混淆了服务器文件系统上的当前工作目录和 Web 服务器文档根目录。
当您在 HTML 中创建图像元素时,它(浏览器)会根据一些参数查找源。
元素 URL(如果已设置)加载,否则为当前 URI将从 http://example.com/foo/bar/baz.jpg
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.
<base>
element URL if set, otherwise the current URI<img src="/foo/bar/baz.jpg">
will load from http://example.com/foo/bar/baz.jpgimg 标签被发送到客户端。
更改预处理器的目录不会更改客户端的目录,因为该目录固定为它们所在的当前页面,例如 http:// /example.com/。
您需要更改每个 img 标签的 src 来更改要查看的目录。
为了避免将来的混乱,您可以使用一个为正确目录添加前缀的函数。
例如
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.
什么是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
jnpcl 说得对:
把它放在你的
之间。
通常在之后,加载任何文件之前
jnpcl had it right:
put that between your
<head> </head>
typically after<meta />
, before you load any files