Php require file in different directory
There are several ways to achieve this. Show
Use relative path from form.php
If you're using PHP 5.2 or older, you can use Use the $_SERVER['DOCUMENT_ROOT'] variableThis is the absolute path to your document root: The document root is the folder
where your root level files are: http://example.org/index.php would be in Usage: Use an autoloaderThis will probably be a bit overkill for your application if it's very simple. Set up PHP's include pathsYou can also set the directories where PHP will look for the files when you use Usage: Note: I see some answers suggest using the URL inside a PHP Require File statement is identical Include File statement (except upon failure it will also produce a fatal E_COMPILE_ERROR level error). PHP Require File Statementrequire 'myfile.php';PHP require file from parental directory (folder) - relative pathrequire(dirname(__DIR__).'/../ myfile.php');PHP Require File from ROOT directoryrequire($_SERVER['DOCUMENT_ROOT']."/myfile.php");PHP Require File deeper directory then ROOT - absolute path$root = realpath($_SERVER["DOCUMENT_ROOT"]); require "$root/folder1/folder2/myfile.php";(PHP 4, PHP 5, PHP 7, PHP 8) The See the include_once documentation for information
about the bimal at sanjaal dot com ¶ 11 years ago
bobray99 at softville dot com ¶ 4 months ago
boda0128318 at gmail dot com ¶ 1 year ago
powtac at gmx dot de ¶ 7 years ago
apis17 ¶ 4 years ago
$a=1;$a<=5;$a++){ Konstantin Rozinov (krozinov[at]gmail) ¶ 13 years ago
$end_time = microtime(true);$handle = fopen("/tmp/results", "ab+"); Mental_Wanderer ¶ 4 years ago
foo() miqrogroove ¶ 17 years ago
ivan[DOT_NO_SPAM]chepurnyi[AT]gmail ¶ 13 years ago
inci szlk ¶ 7 years ago
thomas dot revell at uwe dot ac dot uk ¶ 17 years ago
jazfresh at hotmail.com ¶ 15 years ago
jason semko at gmail dot com ¶ 12 years ago
antoine dot pouch at mcgill dot ca ¶ 16 years ago
amcewen at look dot ca ¶ 14 years ago
spark at limao dot com dot br ¶ 11 years ago
glob($path) as $file){
acholoc at gmail dot com ¶ 8 years ago
Anonymous ¶ 18 years ago
info at erpplaza dot com ¶ 11 years ago
rejjn at mail dot nu ¶ 16 years ago
manuel schaffner ¶ 15 years ago
Pure-PHP ¶ 17 years ago
ulderico at maber dot com dot br ¶ 17 years ago
sneskid at hotmail dot com ¶ 15 years ago
jtaal at eljakim dot nl ¶ 17 years ago
How to require a file in a different folder PHP?Need to load another file in PHP, but it is in a different folder? No problem, it is a very simple fix. The easiest way to include a file from another folder is to use the absolute path (specify the full path to the file). For example, include "C:\\http\\lib\\script.
How do I change the directory of a file in PHP?Creating Directories in PHP
A new directory can be created in PHP using the mkdir() function. This function takes a path to the directory to be created.
What is __ DIR __ in PHP?The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.
What is require_ once in PHP?The require_once keyword is used to embed PHP code from another file. If the file is not found, a fatal error is thrown and the program stops. If the file was already included previously, this statement will not include it again.
|