views:

197

answers:

5

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

A: 

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');

Ian Devlin
+4  A: 

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.

wojo
Wouldn't you have to adjust that path whenever you did the copy though? To me that solution doesn't look much different than just including with a relative path in the first place.
Svish
It's determined at runtime, so you don't need to change it when you copy the directory. During execution it figures out the current file, strips the filename and appends template.php at the end. This is the most resilient and explicit way to include/require a file in PHP I believe.
wojo
ok, but my index.php files will be dynamically created... so i could have a folder structure like this "articles/article_1/subfolder/index.php" and my template folder will be at the same level as "articles" folder
kmunky
But it would just get you the current directory. And adding `/template.php` to that, is nothing more than doing `include('template.php')`. If I understood correctly, his template.php stays in the web root. His files doesn't.
Svish
that's right Svish
kmunky
I misunderstood the original question. Updated.
wojo
ok, but how secure is modifying the htaccess ?
kmunky
Providing a .htaccess file with just that configuration change doesn't loosen the security at all, assuming your web host is configured correctly and the access permissions are correct on the .htaccess file.
wojo
+5  A: 

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)

Svish
+1  A: 

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.

ianhales
`dirname(__FILE__)` won't really get you anywhere that `.` can't get you (unless I have misunderstood something).
Svish
+1  A: 

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').

Kaivosukeltaja
"This is highly discouraged as it opens doors for security breaches." **indeed**
KyleFarris