Confused about root directory on server?

  • aloof
  • Newbie
  • Newbie
  • aloof
  • Posts: 14
  • Loc: Oregon

Post 3+ Months Ago

On my server the root directory is /var/www/html

While using any PHP-related code such as the following:
Code: [ Select ]
<?
require "/var/www/html/template/somefile.css";
?>
  1. <?
  2. require "/var/www/html/template/somefile.css";
  3. ?>

The root directory works fine, and the file is found. But in general HTML code not enclosed within PHP such as the following:
Code: [ Select ]
<link href="/var/www/html/template/somefile.css">

or...

<img src="/var/www/html/images/someimage.jpg">
  1. <link href="/var/www/html/template/somefile.css">
  2. or...
  3. <img src="/var/www/html/images/someimage.jpg">

The files can't be found. The directories exist, and it works if i use the following:
Code: [ Select ]
<?
echo "<img src='/var/www/html/images/someimage.jpg'>";
?>
  1. <?
  2. echo "<img src='/var/www/html/images/someimage.jpg'>";
  3. ?>

but, obviously that is sort of a wonky workaround.

Can anybody tell me why PHP can use the root directory just fine but HTML cannot?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Tom the Great
  • Expert
  • Expert
  • User avatar
  • Posts: 727
  • Loc: B.C., Canada

Post 3+ Months Ago

I think it has to do with where the html file is.
If your index.html was in "/var/www/html/" and you had that code, it would look in "/var/www/html/var/www/html/images/someimage.jpg"
  • cmontana
  • Novice
  • Novice
  • User avatar
  • Posts: 18

Post 3+ Months Ago

It's been my experience that PHP can handle server paths (the location of a file on your server), whereas HTML only handles URLs (the location of a file on the Web). The two are quite different from each other.

Within your HTML code, only use this model:
<img src="images/someimage.jpg"> (an example of relative path)
<img src="http://www.mywebsite.com/images/someimage.jpg"> (an example of absolute path)
And don't forget your height, width and Alt tags :)

Besides, displaying your full server path anywhere in your HTML code (which anyone can read) would create a security risk.
  • Axe
  • Genius
  • Genius
  • User avatar
  • Posts: 5739
  • Loc: Sub-level 28

Post 3+ Months Ago

PHP handles server paths for local access.

<a> and <img> tags are not local access. Those are REMOTE access used by your browser.

If /var/www/html/ is your LOCAL root directory, then that would be / as your remote directory.

/var/www/html/images would simply be /images.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 73 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.