1

Right now my file structure looks like this:

root
    /images
        /banner.png
    /dir
        /file.php
    /index.php

I'm working in file.php. I'm trying to access banner.png, and so far I have this:

<?php echo dirname( dirname(__FILE__) ) . '/' . $recent1["Image"];?>

Where $recent1["Image"] is images/banner.jpg.

When I echo the code as plain text, this path shows up:

/home/htdocs/images/banner.png

Which I feel like is right, but I'm not sure if it's the exact same thing as

(root)/images/banner.png

Again, I feel like those two are the same thing, but the image isn't displaying when I put that code I showed above in for the src attr for an img tag. It shows that broken picture icon thing. Yes, I'm sure that images/banner.png exists. Thanks for the help in advance.

Edit: I also might add that in index.php, using just $recent1["Image"] does indeed display the image.

Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
wwohlers
  • 215
  • 1
  • 3
  • 7
  • 1
    The simple answer is if the image does not show the path or filename is probably wrong. However you dont show how this looks in the browser? Use `View Page Source` to see what is getting to the browser – RiggsFolly Jun 04 '16 at 15:04
  • 1
    That path is wrong, because URLs that are sent to the client should start at the web root, not the server's real root. It should be `/images/banner.png` – Barmar Jun 04 '16 at 16:10

3 Answers3

0

Try using a different Magic constant

I think you're looking for

__DIR__

Using your provided example

  <?php echo __DIR__ . '/' . $recent1["Image"];?>
Marc Barbeau
  • 826
  • 10
  • 21
  • Although I worked around the problem completely, I'm fairly sure this would have fixed it. Good to know for the future, thanks. – wwohlers Jun 04 '16 at 15:36
  • The documentation says **This is equivalent to dirname(__FILE__).** – Barmar Jun 04 '16 at 16:11
0

Instead of __FILE__ you should use $_SERVER['PHP_SELF']. The documentation explains:

The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/foo/bar.php would be /foo/bar.php.

<?php echo dirname( dirname($_SERVER['PHP_SELF']) ) . '/' . $recent1["Image"];?>

You could also use $_SERVER['SCRIPT_FILENAME']. They could be different if you have URLs like /dir/file.php/foo/bar (this is sometimes preferred for SEO reasons, although it's often rewritten in .htaccess to /dir/file.php?param1=foo&param2=bar)

You could also use a relative path:

<?php echo "../" . $recent1["Image"]; ?>
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • `PHP_SELF` should not be used, there are far better Magic Constants available as PHP_SELF takes the current value in the browser URL such as `/images/file.php?horse=yes&script=no` would be the value for `PHP_SELF` on this page, when infact something akin to `$_SERVER['SCRIPT_FILENAME']` would give the better value of `/images/file.php` (which also can not so easily be adjusted by the visitor editing their own destinaton URL – Martin Jun 04 '16 at 16:55
  • Not quite. The query string is not in `PHP_SELF`, it's in `REQUEST_URI`. The only difference between `PHP_SELF` and `SCRIPT_FILENAME` is when you have a URI like `/dir/file.php/foo/bar`. http://stackoverflow.com/questions/279966/php-self-vs-path-info-vs-script-name-vs-request-uri – Barmar Jun 04 '16 at 17:13
  • @Martin I've added something about `SCRIPT_FILENAME` to the answer. – Barmar Jun 04 '16 at 17:26
0

The main issue in the code provided is that you are calling dirname() twice.

<?php echo dirname( dirname(__FILE__) ) . '/' . $recent1["Image"];?>

"Given a string containing the path of a file or directory, this function will return the parent directory's path that is levels up from the current directory." http://php.net/manual/en/function.dirname.php

Example

Given that __FILE__ results to '/home/htdocs/index.php'.

dirname(__FILE__) ='/home/htdocs/'

dirname('/home/htdocs/'); ='/home/'

Then, you append the image path '/images/banner.jpg'

Results in

'home/images/banner.jpg'

When the path really should be

'/home/htdocs/images/banner.jpg'
Marc Barbeau
  • 826
  • 10
  • 21