Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include files from parent or other directory

Tags:

php

I'm needing to include a file from the parent directory, and other sub-directories, into a sub-directory. I've done it before by simply using include('/rootdirectory/file.php'); but now it won't seem to work.

Just wondering how I can do this, thanks.

Here's my exact line:

include('/forums/groups.php'); 

It's giving me this error(the page still runs):

Warning: include(/forums/groups.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\forums\blog\posts.php on line

Warning: include() [function.include]: Failed opening '/forums/groups.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\forums\blog\posts.php on line 3

like image 941
Darren Avatar asked Jan 11 '12 21:01

Darren


People also ask

HOW include file in parent directory in php?

If the referenced object is a folder, the function will return the parent folder of that folder. For the current folder of the current file, use $current = dirname(__FILE__); . For a parent folder of the current folder, simply use $parent = dirname(__DIR__); .

What is the parent directory of a file?

2. With a directory, a parent directory is a directory containing the current directory. For example, in the MS-DOS path below, the "Windows" directory is the parent directory of the "System32" directory, and C:\ is the root directory.

Why php include is not working?

You need to try the path of functions. php within the system and not its url. Do you have console access? If so just find out what directory is the file in and include it using the full path.

How do I navigate to parent folder?

There is no way to go to the parent folder right out of the search results, as that is not how it operates, however if you double-click on the quick launch search result, resulting in navigating to the folder, then press alt+up then it will go to the parent folder.


2 Answers

include() and its relatives take filesystem paths, not web paths relative to the document root. To get the parent directory, use ../

include('../somefilein_parent.php'); include('../../somefile_2levels_up.php'); 

If you begin with a /, an absolute system file path will be used:

// Full absolute path... include('/home/username/sites/project/include/config.php'); 
like image 112
Michael Berkowski Avatar answered Sep 22 '22 11:09

Michael Berkowski


If your server is not resolving the file from the parent directory using

include '../somefilein_parent.php'

try this (using the parent directory relative to the script):

include __DIR__ . "/../somefilein_parent.php";

like image 36
Steven Spungin Avatar answered Sep 19 '22 11:09

Steven Spungin