So, I have a directory structure like this:
include/
scripts/
contact.css
contactform.php
ReadMe.txt
show-captcha.php
thank-you.php
In the show_captcha.php file the following is added to include PHP file from the /include subdirectory.
require_once("./include/fgcontactform.php");
Thise code works perfectly.
My question here is shouldn't it be like this:
require_once("include/fgcontactform.php");
Without the ./ prefix? What is the meaning of this path?
Both
require_once("./include/fgcontactform.php");
and
require_once("include/fgcontactform.php");
mostly do the same thing.
The '.' indicates the current working directory.
./include/fgcontactform.php means you're including from current working directory.
When your requirement is to include from other directory you do something like this -
../PATH_TO_DIRECTORY/include/fgcontactform.php
Related
I'm facing a problem using relative path...I have that directory structure
-dev
--vendor
-autoload.php
--includes
-index.php
now i need to include autoload.php in index.php
As adviced on previous answers i tried include('../vendor/autoload.php'); but it didnt work.
but this didnt work so i had to use this walkaround require_once($_SERVER['DOCUMENT_ROOT'] . '/dev/vendor/autoload.php');
So im wondering why the relative path didnt work and how to make it work?
The only thing that comes to my mind right now is the current working directory.
If you don't specify an absolute path the current working directory will be chosen.
E.g.
<?php
chdir("/tmp");
include "test.php"; // will include /tmp/test.php
And
<?php
chdir("/home/marco");
include "test.php"; // will include /home/marco/test.php
Try changing your path to: __DIR__."/../vendor/autoload.php".
See PHP: Magic Constants for further reference.
So I have a web application hosted on a site. The web root (I.E. the files the client can access) is the public_html folder. However, I need to include files outside of the public_html folder. I do this using php include. I get an error no such file or directory. When it shows me the path it is still looking in the public_html folder, which is not where I need it looking.
The code looks like this:
<?php include('../eCommerceCore/shoppingCart.php');?>
I need it to look up one level but it will not search outside of public_html. Also, the file containing the line of code shown above is in the public_html folder if that helps.
Sometimes the current directory isn't what you expect it to be, such as when you include a file from an included file.
I like to use $_SERVER['DOCUMENT_ROOT'] on my includes so that I can always reference them absolutely from the root of my site:
<?php
include($_SERVER['DOCUMENT_ROOT']."../eCommerceCore/shoppingCart.php");
?>
Or this way you can try
<?php
include "../eCommerceCore/shoppingCart.php";
?>
If your includes directory is above your document root, you can use .. to still reference from the root.
I have a file structure that looks like this (unimportant files left out):
Testserver
-file.php
PHP
-functions.php
Administration
-index.php
Now when I try to call require_once('../PHP/functions.php'); inside the "index.php" it doesn't work and I don't understand why. Anyone an idea what the problem could be?
This works fine:
require_once('../file.php');
first thanks for all the answers, I found the bug now:
inside the functions.php I require another file and the path to that file wasn't correct when I called it from index.php
So I had to change the path inside functions.php not inside index.php.
Still thanks to everyone for the other suggestions :)
Do you get an PHP error, and if what does it say?
Check for lowercase/uppercase problems
The following should work correctly:
require_once('../PHP/functions.php');
Check your privileges on the folder and try:
require_once('../PHP/functions.php')
You have a Folder Structure Like so:
Testserver
-file.php
PHP
-functions.php
Administration
-index.php
And then, you are inside of index.php trying to Access the File functions.php, which lives inside of the Directory PHP. Then you need to go up one Directory from the current directory of the index.php File (ie. The Administration Directory) [meaning that you get up to Testserver Folder from theAdministration Folder] and then drill down to the PHP Directory. In code this would mean:
<?php
require_once __DIR__ . "/../PHP/functions.php";
Hope this does it for you.
Cheers & Good Luck....
I'm unable to include files in a folder from the root. Here's something similar to what I'm trying to do.
Root:
-folder1
--folder2 (I need to include stuff in this folder)
-otherfolder1
--otherfolder2
--otherfolder3 (This is my website folder)
This is what I have for my path so far which doesn't work.
define("FOLDER2_ROOT", "/folder1/folder2/");
../ is used to go up one folder.
So according to your situation, you need the following code:
include("../../../folder1/folder2/abc.php");
I have a header.php that I'd like to include on all pages of the script I'm making. It has the nav bar and header.php includes auth.php which is some basic authentication you'd expect a header page to have.
The problem I am having is relative paths vs absolute paths. My directory structure looks like the below
/root
-index.php (contains include ('inc/header.php');)
-auth.php
/inc
-header.php (contains include ('auth.php);)
Now this works perfectly well. However when I add some functionality and get the following directory structure things start to break
/root
-index.php (contains include ('inc/header.php');)
-auth.php
/inc
-header.php (contains include ('auth.php);)
/newFunctionality
-newStuff.php (contains include('../inc/header.php') but breaks because of relative paths fidning auth.php
I've played around with things like $fullPath = dirname(__FILE__); and I believe that this is most likely how I'm going to fix this problem.
I've seen very similar questions with answer like this. I'd like for this script to be independent of the index however. Is there a way to make this happen?
I've alway found it easiest to set a base path constant and include my top level scripts base on that
define ('APP_ROOT', '/path/to/application/root');
include(APP_ROOT."/libs/MyFile.php");
Normally I would put this (along with any other site constants) in an include file and include it relative to the either the document root
include($_SERVER["DOCUMENT_ROOT"]."/config/site.php");
or specify the location of the file in my .htaccess
SetEnv APP_CONFIG /path/to/application/config.php
then in php
include ($_SERVER["APP_CONFIG"]);