kmunky
kmunky

Reputation: 15583

PHP include file

i have two files:(localhost/template/)

index.php 
template.php

each time when i create an article(an article system is what i'm trying to do),i create a folder and then i copy the index.php in that folder. I want to include template php in index.php but as a static url('cause the articles will be like a folder-subfolder/subfolder/.. structure )

i tried: include('localhost/template/template.php') with no result. how should i include it? thanks

Upvotes: 0

Views: 1070

Answers (5)

Svish
Svish

Reputation: 158071

The include method works on the file system path, not the "url path". Solution is to either

  • Give it an absolute path.
    -- include('/some/path/to/template.php');

  • Change the relative path so it is correct after each copy you create.
    -- include('../../template.php');

  • Change the include path of PHP so that the file is in, well, the include path.
    -- Can be done either in the php.ini file, in the .htaccess file or with the set_include_path function. (Depending on how much you want to set it for, and what you have permission for)

Upvotes: 5

wojo
wojo

Reputation: 11801

You could include it relative to the current directory, like so:

require_once(dirname(__FILE__) . '/template.php');

dirname(FILE) will translate to the directory of the current script (index.php) and the line above will append '/template.php' resulting in the full path to the template.php side-by-side to the index.php file.

I find it best to include files this way vs without a full path to avoid issues with the PHP search path, for example. It's very explicit this way.

UPDATE: I misunderstood the original question. It sounds like template.php isn't copied, only index.php is. So you'll have something that could be like:

template/template.php
template/index.php (just a template)
foo/bar/index.php
foo/bar2/index.php

Since people can hit the foo/bar/index.php for example without funneling through a central script, you'll have to somehow find the template no matter where you are.

You can do this by setting the PHP include_path, for example through a .htaccess on a Apache server:

php_value include_path ".:/home/<snip>/template"

Then in each index.php you can include template.php and it'll search the current directory first, then try your template directory.

You could also compute the relative path when copying the script and put an include in there with the proper number of '..' to get out (e.g. '../../template/template.php'). It's kinda fragile, though.

Upvotes: 4

Kaivosukeltaja
Kaivosukeltaja

Reputation: 15735

If you're trying to include the file as an url, you'll need to start it with http:// and have allow_url_include set to true in PHP settings. This is highly discouraged as it opens doors for security breaches.

Instead, you should either add localhost/template to your include path or use relative urls like include('../template.php').

Upvotes: 1

n00dle
n00dle

Reputation: 6043

You don't want the "localhost" in there. Includes work using the actual path on your server. So you can either use relative ones such as posted above, or absolute in terms of server so this could be "/opt/www/" on linux or "c:\Program Files\Apache\htdocs" on windows. This will be different from system to system so to find out yours use the dirname(__FILE__) technique shown by wojo.

Upvotes: 1

Ian Devlin
Ian Devlin

Reputation: 18870

The path looks wrong, you should include it with a path relative to where the calling file is, e.g. include('template/template.php'); or include('../template/template.php');

Upvotes: 0

Related Questions