13

I used:

php_value auto_prepend_file "file.php"

in my .htaccess that is in public_html folder.

Now when I run public_html/sub/index.php i get this error :

Fatal error: Unknown: Failed opening required 'file.php'

How to use auto_prepend_file flag to include a file relative to .htaccess file ?

5
  • 1
    why not use absolute path : /home/user_name/public_html/file.php ? Commented Jan 28, 2012 at 13:16
  • @ZulkhaeryBasrul because i don't know what is the absolute path that software is installed. Commented Jan 28, 2012 at 13:24
  • 1
    Can't you use get_cwd() on the php script to find the absoulute path or $_SERVER['DOCUMENT_ROOT'] ? Commented Jan 13, 2013 at 23:12
  • @MyStream So that's using PHP and I'm trying to use htaccess Commented Jan 14, 2013 at 18:16
  • I meant to get the path - not to use that command in your htaccess - just to find the path for inclusion in your .htaccess - sorry for the confusion =]. Commented Jan 15, 2013 at 3:24

3 Answers 3

18

The file must be inside PHP's include_path. So you must either set the file's directory to be in the include_path inside php.ini, or do it in the .htaccess with a php_value statement.

php_value include_path ".:/path/to/file_directory"
php_value auto_prepend_file "file.php"

If you use the above method in .htaccess, be sure to copy the include_path from php.ini in and add the :/path_to/file_directory so you don't lose any already needed includes.

Alternatively, just add :/path/to/file_directory to include_path directly in the php.ini

Update

If you cannot modify the include_path, you might try specifying a relative path to the auto_prepend_file. This should work since the file path sent is processed identically as if it was called with require():

php_value auto_prepend_file "./file.php"
Sign up to request clarification or add additional context in comments.

2 Comments

Can you use an absolute system path? If so, get the current system path - perhaps use phpinfo() to find it - and use that?
For those working in two environments (development / production). You can easily include two paths, like so: .:/path/for/development:/path/for/production.
10

In your .htaccess

php_value auto_prepend_file /auto_prepend_file.php
php_value auto_append_file /auto_append_file.php

Next create 2 files in root

1) /auto_append_file.php

$appendFile = $_SERVER['DOCUMENT_ROOT'] . '/append.php';
require_once($appendFile);

2) /auto_prepend_file.php

$prependFile = $_SERVER['DOCUMENT_ROOT'] . '/prepend.php';
require_once($prependFile);

Now it should work on local and live servers irrespective of physical path or website providing each of your websites use the same filenames append.php and prepend.php.

Comments

4

I would also add, that if you don't know the hard relative server paths (as with shared hosting and/or PaaS which generate paths dynamically on reboot/deploy) then you can do this:

php_value include_path ./:../:../../:../../../:../../../../
php_value auto_prepend_file "prepend.php"

In essence this is Pseudo dynamic method/hack/workaround for achieving a .htaccess relative DOCUMENT_ROOT include (which is NOT possible in Apache) as follows:

php_value include_path "%{DOCUMENT_ROOT}/"

For security, in prepend.php, it's then possible to re/declare include path as follows (to whatever appropriate paths fit the application):

ini_set('open_basedir',$_SERVER['DOCUMENT_ROOT'].'/':<etc.>);

Could also look like the following if the last few directories are predictable:

php_value include_path ../../../path/to/www/
php_value auto_prepend_file "prepend.php"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.