views:

31

answers:

3

Hey all,

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

+1  A: 

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

Jason McCreary
+4  A: 

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
Phil Brown
Matt
@Matt Absolute URI is `http://www.domain.com/foo/bar/baz.jpg` where Absolute path is `/foo/bar/baz.jpg`.
Tom
Gotcha, thanks!
Matt
+2  A: 

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.php'); ?>" width = "402" height = "265" alt = "1" />
Thomas F.