I am trying to create a script and I want to be able to say

require "~/functions/functions1.php";
require "~/functions/functions2.php";
require "~/functions/functions3.php";

but php doesn't seem to understand what the tilde means (and yes this for a linux server). Is there any way I can specify the home dir without actually having to point it to to absolute path? A relative path wouldn't work because I won't know how many folders some of these files will be in

Dani AI

Generated

Short answer: PHP does not perform shell-style tilde expansion. The shell expands ~ when you type ls ~ at a prompt, but include/require paths in PHP are treated as literal strings, so ~ won't be translated into a home directory. That is the immediate reason your includes fail.

Practical fixes that are reliable across environments:

  • Anchor includes to the script filesystem location (best practice). Use __DIR__ (PHP 5.3+) with a dirname(__FILE__) fallback for older PHP versions, and build filesystem paths from that base.
$base = defined('__DIR__') ? __DIR__ : dirname(__FILE__);
require $base . '/lib/myfunctions.php';
  • Or set the include path once (bootstrap) and then require filenames without long paths:
set_include_path($base . '/lib' . PATH_SEPARATOR . get_include_path());
// later:
require 'myfunctions.php';

Other notes and gotchas:

  • For CLI scripts getenv('HOME') or POSIX helpers (if available) can give a home directory, but these are unreliable for web SAPI (the webserver user’s HOME may differ or be unset).
  • Avoid using HTTP URLs in require/include unless you understand allow_url_include and the security implications — most setups disable it. ’s global-path idea is fine, but prefer a filesystem path (not an http:// URL).
  • Server restrictions (open_basedir, filesystem permissions, chroot) can also prevent access to files outside allowed directories — as hinted, that’s a separate, server-side constraint to check.

Troubleshooting tips: dump __DIR__, getcwd(), get_include_path() and ini_get('open_basedir'), and use file_exists()/is_readable() before requiring to see what PHP actually sees. The simplest, most portable approach is to define one canonical base directory in a bootstrap and use absolute filesystem paths built from it.

Recommended Answers

All 4 Replies

One method would be to define a global var to hold the absolute path to the specified folder. You can then use the global var in the require function to build the path to the file required. Example:

define('FUNCTION_PATH', 'http://127.0.0.1/devsite/functions/');
// the FUNCTION_PATH var can then be used like:
require(FUNCTION_PATH.'functions1.php');
require(FUNCTION_PATH.'functions2.php');
require(FUNCTION_PATH.'functions3.php');

There are more than likely server configurations that are preventing you from accessing the file in ~/functions/. This is primarily due to security issues.

Also, you probably should specify which user's home directory you want to go from (i.e. ~johndoe/functions/).

-Fredric

There are more than likely server configurations that are preventing you from accessing the file in ~/functions/. This is primarily due to security issues.

Also, you probably should specify which user's home directory you want to go from (i.e. ~johndoe/functions/).

-Fredric

The reason I did it that way was because I can easily go type the following in SSH and it works just fine

ls ~

so it's not which user, the tilde... (at least as I understand it) searches your OWN home dir so there is no need to specify which user at least on the servers I am working on

I am trying to create a script and I want to be able to say

require "~/functions/functions1.php";
require "~/functions/functions2.php";
require "~/functions/functions3.php";

but php doesn't seem to understand what the tilde means (and yes this for a linux server). Is there any way I can specify the home dir without actually having to point it to to absolute path? A relative path wouldn't work because I won't know how many folders some of these files will be in

I was really trying to having to avoid doing the absoluate path, even though I know I could call on $_SERVER to generate the path for me in the example you gave. So is there no way to specify your own home dir in PHP?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.