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....
Related
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.
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
I've encountered something when trying to include/require a php script that's 2 directories back. It's not really a problem as I figured out a work around, however I'd love an explanation for what's happening.
Here's the file structure:
appCode
db.php (File I'm trying to include)
studentManagement
index.php
dep
getData.php (File I'm trying to include db.php into)
I want to include appCode/db.php in studentManagement/dep/getData.php.
getdata.php is executed with ajax from index.php
When I use:
require_once("../../appCode/db.php");
It doesn't work.
The only way it works is it I change directory first:
chdir("../");
require_once("../appCode/db.php");
Why won't the first method work? I've also tried using include instead of require but it's the same. I'm testing it on mamp 3.0.4.
Any help appreciated!!
that is because when you require(),include() and their variants it's always relative to the initial php file called (in your case index.php)
in fact chdir has nothing to do with it, and this:
require_once("../appCode/db.php");
is the right way to call it.
always place your mental self (!) as if you were index.php when you require files and work with directories. your "active directory" is the one where index.php is placed. you can always verify this current working directory with getcwd() http://php.net/manual/en/function.getcwd.php
If you know your entire directory all the way from root you can use:
http://php.net/manual/en/function.set-include-path.php
Now instead of using relative paths you can always include or require files starting at your include path.
You could put this in getData.php:
set_include_path ('/homepages/www/2/yourworkingpath');//Use your own directory
require_once 'appCode/db.php';
You could also do the same thing in your other files if you need to include and it will always use your include path so you don't have to keep figuring out which directory to change to. Hopefully this helps a bit.
following is my files directory structure...
Config
config.php (is calling some files too like includes/class.DB.php)
Now i have created another folder Admin and created new config.php (and calling root config file by require_once '../config/config.php';) its loading config file correctly but showing errors on includes/class.DB.php .
I hope, you have got my an idea of my problem, what is the way to achieve this, by USING DIR_NAME/ $SERVER['DOCUMENT_ROOT']
please help.
use
dirname(__FILE__); // or, if you have +5.3, use __DIR__ instead
so if you have this structure
includes
-class.php
admin
-admin.php
config
-config.php
So you can use
//admin.php
include(dirname(__FILE__)."/../config/config.php");
//config.php
include(dirname(__FILE__)."/../includes/class.php");
it always aims to the same directory!
Try:
include(dirname(__ FILE__)."/../include/connection.php");
Please help!
My directory structure is this:
my_application
my_application/index.php
my_application/images
my_application/phpcode/my_script.php
I have this code in my_script.php
<?php
if(file_exists('../images)){
// do something
}
?>
It works in wamp perfectly!!!
But when I upload it to my server (hostgator), it doesn't work.
In my server I do not upload it to root folder, but to a subfolder.
rootfolder/folder1/folder2/my_application
From what I understand, .. is in reference to the root path, not the current file.
How can I reference the current's file parent's folder images?
Sorry for my English. Please help! I'm stuck on this one.
You can simpli use dirname($SCRIPT_FILENAME) to reference the direcotry of your current script.
Have a look at PHP Constant and PHP Reserved Variable :)
Hope this help