开发者

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.

  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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜