A Small Orange Forums: PHP include - root relative link issues - A Small Orange Forums

Jump to content

NOTICE: This is *not* an official support forum

All support requests should be made by through our Support Desk or by emailing help@asmallorange.com.
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

PHP include - root relative link issues Be wary, PHP noob. Rate Topic: -----

#1 User is offline   OrganizeFISH

  • Tiny Orange
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 19-August 06

Posted 07 December 2006 - 04:17 AM

I've created a basic template in which I use five include statement, four of which are PHP includes. The fifth calls the CSS file and is the only one that works.
CODE
<link type="text/css" rel="stylesheet" href="/includes/css/main.css" />

From what I thought I understood, using the following would start from the root:
CODE
<?php include("/includes/incHeader.php"); ?>

Yet, this is what results: redesign.theanswerband.com/pages/demos.php
QUOTE

Warning: main(/includes/incHeader.php) [function.main]: failed to open stream: No such file or directory in /home/theanswe/public_html/redesign/pages/demos.php on line 18

Warning: main(/includes/incHeader.php) [function.main]: failed to open stream: No such file or directory in /home/theanswe/public_html/redesign/pages/demos.php on line 18

Warning: main() [function.include]: Failed opening '/includes/incHeader.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/theanswe/public_html/redesign/pages/demos.php on line 18
Put page content here.

Warning: main(/includes/incFooter.php) [function.main]: failed to open stream: No such file or directory in /home/theanswe/public_html/redesign/pages/demos.php on line 24

etc, etc

Interestingly, in Dreamweaver it shows up in design mode, but as soon as I hit the button to use the test server on my computer, the same errors show up.

For FYI's sake, if I change the include statements to not include the preceeding slash mark...
CODE
<?php include("includes/incHeader.php"); ?>
...then the home page renders: redesign.theanswerband.com (currently has code w/preceeding slash mark).

My file structure is:
[public_html]
--->[redesign] (subdomain)
------>index.php
------>[includes]
--------->incHeader.php
--------->[css]
------------>main.css
------>[pages]
--------->demos.php
0

#2 User is offline   ukdmbfan

  • Here come the bugs
  • PipPipPipPipPip
  • Group: Members
  • Posts: 416
  • Joined: 17-November 05

Posted 07 December 2006 - 12:35 PM

Basically, HTML HREF's are relative to the URL root (ie, where the public_html folder points to in your domain name). So, for example, all HREF's are relative to the domain folder they're being called from, ie:

http://www.mydomain.com/me/you.jpg -> /me/you.jpg

"./" is the same as using no slash at all (in most situations, although it's always better to use ./ than nothing), so if you want to access something from the folder your in (./) you can use that, for example

http://www.mydomain.com/file.html has a link to /me/you.jpg which could also be ./me/you.jpg or me/you.jpg

PHP paths are relative to the root path of your hosting, which on linux is / - your public_html folder (where all your website files are stored) has the path /home/ASOusername/public_html/ - therefore / in PHP is relative to this.

For example, if you had a file that in your URL was http://www.mydomain.com/style.css, that would appear in your directory structure on your server as /home/ASOusername/public_html/style.css. This is where your PHP paths need to be relative to, not your domain URL.

The ./ and no-slash principles also apply to PHP paths, so if you're running a PHP file on your main domain (ie http://www.mydomain.com/me.php) the directory PHP is in would be /home/ASOusername/public_html. Therefore you can use ./style.css to include a file in PHP as well as /home/ASOusername/public_html/style.css.

It's also worth noting that the tilde (~) will always give you your home directory just in case it's not exactly what's written here (like if your path is /home2/ASOusername/ for example). ~ will point to /home/ASOusername/, so it's probably better to use this, ie:

http://www.mydomain.com/me.php = /home/ASOusername/public_html/me.php = ~/public_html/me.php

This post has been edited by ukdmbfan: 07 December 2006 - 12:35 PM

"If you do things right, people won't be sure you've done anything at all"
web fanattic.it email matt at fanattic.it
0

#3 User is offline   OrganizeFISH

  • Tiny Orange
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 19-August 06

Posted 07 December 2006 - 04:11 PM

Am I correct in understanding that "./" = no slash, and "~/public_html" (for php) = "/"? Does the tilde ("~") apply in a similar manner for HTML HREF? I.e. "~/" = "/". I ask because I can't seem to get the tilde to work.

Basically, I changed
CODE
<?php include("/includes/incHeader.php"); ?>

to
CODE
<?php include("~/public_html/includes/incHeader.php"); ?>

...and it stopped showing up in Dreamweaver's design mode AND I still received the same PHP error. Basically, the "~/public_html/REST/OF/PATH" doesn't work.

By the way, when testing in Dreamweaver, to accomodate the testing server on my computer, I actually changed the code to
CODE
<?php include("~/htdocs/includes/incHeader.php"); ?>

So, actually, I guess I have two issues now. Getting me to understand this file path business, and what I now get is my real issue:
CODE
Warning: main(/includes/incHeader.php) [function.main]: failed to open stream: No such file or directory in /home/theanswe/public_html/redesign/pages/demos.php on line 18

I found the following from http://www.zen-cart....ndex.php/Errors:
QUOTE
Warning: main(<...>): failed to open stream: No such file or directory in <...>

First make sure you have uploaded all files - specifically the file in question - and set correct permissions.

If that doesn't fix it, check your PHP include_path, which should be listed along with the error message you got. You can also check this setting with phpinfo().
On a windows server, the include_path needs to start with

.;

and on a *nix server, it must start with

.:

If the include_path does not contain this, you must configure it in php.ini or in a .htaccess file. If in doubt about how to do this, ask your host.

* php.ini
For Windows:

include_path = ".;Drive:\path\to\php\includes"

For *nix:

include_path = ".:/path/to/php/includes"

* .htaccess
For Windows:

php_value include_path ".;Drive:\path\to\php\includes"

For *nix:

php_value include_path ".:/path/to/php/includes"


If that is my issue, why is that when I used the document relative link ("includes/incHeader.php") as opposed to the site root relative link ("/includes/incHeader.php"), the index page worked?

(Also, I tried to edit my .htaccess with...
CODE
php_value include_path ".:/usr/lib/php:/usr/local/lib/php:/home/theanswe/public_html/"
...and all I ever got was an Internal Server Error.)

Here is a link to my phpinfo file: redesign.theanswerband.com/phpinfo.php.
0

#4 User is offline   adamrice

  • Tiny Orange
  • Pip
  • Group: Members
  • Posts: 8
  • Joined: 19-November 06

Posted 07 December 2006 - 07:56 PM

QUOTE(OrganizeFISH @ Dec 7 2006, 3:11 PM) View Post
Am I correct in understanding that "./" = no slash,

Yes. The "." means "this directory." And ".." means "the directory above this." This can be handy if you need to reference something that's in a higher or adjacent directory.
QUOTE
and "~/public_html" (for php) = "/"?
No. The leading "/" always means the root directory of that machine. "~" always means your home directory.
QUOTE
Does the tilde ("~") apply in a similar manner for HTML HREF? I.e. "~/" = "/".

No. An HREF in HTML is something that the browser needs to be able to figure out, and they better not be able to work out the path to root on the server's hard drive! You can use relative paths, of course, by which I mean, if you have two files a.html and b.html in the same directory, you can reference b.html from a.html simply as "b.html" without specifying the whole path. The browser will work it out. Same with parent and child directories using things like "subdir/file.html" or "../higherlevelfile.html" (addressing parent directories pretty much assumes the current file is located in a child directory for that site, but it does work if you are).

This post has been edited by adamrice: 07 December 2006 - 07:56 PM

0

#5 User is offline   ukdmbfan

  • Here come the bugs
  • PipPipPipPipPip
  • Group: Members
  • Posts: 416
  • Joined: 17-November 05

Posted 11 December 2006 - 07:22 AM

My apologies as well, I don't think ~ will work for PHP because ~ means the home directory of the current user that's logged on, and although that will work for SSH (as you login with your ASO username) it won't for PHP because PHP's username isn't that of yours.

The best thing to do is to figure out the path to your public_html directory, either through SSH or using phpinfo(), and then use that in your scripts. It should be /home/ASOusername/public_html/, but it's best to make sure.

Also, as adam noted above, operating system paths and HTML URL's aren't related, they just happen to (most of the time) describe the same directory structure, within your public_html directory. Outside of that (ie, what your web browser can see of your site) it has no idea.
"If you do things right, people won't be sure you've done anything at all"
web fanattic.it email matt at fanattic.it
0

#6 User is offline   Skulled

  • Small Orange
  • PipPip
  • Group: Members
  • Posts: 17
  • Joined: 03-December 06

Post icon  Posted 11 December 2006 - 02:47 PM

FISH, when trying to use php includes, I find the best way normally is to use the full absolute path to the files.

PHP should set its DOCUMENT_ROOT variable to the root folder, so try using the following form of include statements, and that should work on most servers I believe.

CODE
<?php include $_SERVER["DOCUMENT_ROOT"] . "/includes/incHeader.php"; ?>


The DOCUMENT_ROOT variable should automatically always get the right path to your root html folder on the server, and from there you know the file structure ..

Hope that helps.

This post has been edited by Skulled: 11 December 2006 - 02:48 PM

Skulled

"Excuses are like ass-holes, everyone's got one!!"
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users