I have been inspecting code, reading questions on StackOverflow, but I simply don't get it, or rather I dont understand the explanations and / or logic behind it.
Consider The Following
If I have a directory structure like this
Now I want to set the head.php file to be globally accessible throughout the application (just as an example)
define('Head', __DIR__ .'/views/head.php');
If I do the above, I get the following result:
C:\xampp\htdocs\carRental/views/head.php"
Which is technically what I want,, however, notice the URL contains forward and backslashes?
Can I get access to the head.php file by calling Head anywhere in my directory tree?
Im sorry, Ive been inspecting code and read the manual and questions on here, if anyone could give a rookie a clear explanation it would be greatly appreciated.
UPDATE:
When I try to do the following in landingPage.php I get the following errors
include_once Head;
Notice: Use of undefined constant Head - assumed 'Head' in
C:\xampp\htdocs\carRental\views\landingPage.php on line 2
Warning: include_once(Head): failed to open stream: No such file or
directory in C:\xampp\htdocs\carRental\views\landingPage.php on line 2
Warning: include_once(): Failed opening 'Head' for inclusion
(include_path='C:\xampp\php\PEAR') in
C:\xampp\htdocs\carRental\views\landingPage.php on line 2
When you used define('Head', __DIR__ .'/views/head.php'); you have hardcoded the slashes in the definition.
However windows by default uses \ as the default directory separator so __DIR__ will be using \ in the path when in Windows (it's ok with using / as an alternative one though so it shouldn't be a problem).
You can do the following if you want them to be consistent:
define('Head', __DIR__ .DIRECTORY_SEPARATOR ."views".DIRECTORY_SEPARATOR ."head.php");
Which is technically what I want,, however, notice the URL contains forward and backslashes?
yes
Can I get access to the head.php file by calling Head anywhere in my directory tree?
no
The mix of foreward and backward slashes is created by your command
__DIR__ creates C:\xampp\htdocs\carRental and '/views/head.php' is the string you append.
To be able to use your defined HEAD you would have to load the php file which defines it. another php files does not know what this file does or doesn't do as long as it is not persisted. (which you don't do in the code provided)
To load a file and make your definition available use include_once / require_once
__DIR__ will always resolve to the absolute directory of the file using it.
The reason for forward and backslashes. This part:
C:\xampp\htdocs\
Is Windows file path.
This part:
carRental/views/head.php
Is the webserver path, i.e not Windows.
Your define will hold the correct file path so try to include it now:
include_once Head;
Related
I am pretty new in PHP and I have the following problem trying to define a required file into a .php file, I have done in this way:
require("../../common/define.php");
because the define.php is into the common/ two level back related to the file in wich I am instering this require directive.
But this syntax seems to be wrong because it give me this error message:
Warning: require(../../common/define.php): failed to open stream: No such file or directory in C:\xampp\htdocs\PandaOk\templates\default\common\remove-booking_pc.php on line 3
Why? What is wrong? How can I insert a required file that is some level back in the tree?
PHP current file:
C:\xampp\htdocs\PandaOk\templates\default\common\remove-booking_pc.php
So then in this file you have a request for ../../common/define.php
Which is two steps [folders] back, then into the common folder so;
This is two steps back:
C:\xampp\htdocs\PandaOk\templates\
And into the common folder:
C:\xampp\htdocs\PandaOk\templates\common\
So what you've given is a directory that doesn't exist, which is exactly what the error tells you.
instead, only go one step back:
require("../common/define.php");
Alternatively, and far better practise, is to do an absolute file path using $_SERVER['DOCUMENT_ROOT'] such as something like:
require $_SERVER['DOCUMENT_ROOT']."/templates/default/common/remove-booking_pc.php";
This would mean that the reqire always succeeds regardless of where in your project diretory tree it is called from.
(I don't know what directory your document_root will be defined as, but you should get the idea, I hope)
You can also give a full static file path such as commented by Sahil Gulati.
require "C:\xampp\htdocs\PandaOk\templates\default\common\define.php";
P.S: Require and include do not need brackets.
this is kind of a silly question, but as I can't sort it out I thought it might be good to get some help. The point is that the ".. /" to go back directory is not working.
The file I'm executing is in a folder that's on the main route and I need to go back to the main route and then enter another folder to load this other PHP file but it's not working what could be causing this issue.
ERRORS:
Warning: require_once(../PHPMailer/PHPMailerAutoload.php): failed to open stream: No such file or directory in things/public_html/classes/Mail.php on line 3
Fatal error: require_once(): Failed opening required '../PHPMailer/PHPMailerAutoload.php' (include_path='.:/opt/alt/php71/usr/share/pear') in things/public_html/classes/Mail.php on line 3
DIRECTORY STRUCTURE:
File where the requiere once is:
/public_html/classes/filethatwantstoacces.php
File where it wants to get:
/public_html/PHPMailer/PHPMailerAutoload.php
require_once('../PHPMailer/PHPMailerAutoload.php');
What you should be using is the $_SERVER['DOCUMENT_ROOT'] variable. Please read this answer to another question for details.
If you are using PHP you should get into a habit of NOT using relative file paths at all but to use absolute paths, which will guarentee to succeed every time (As long as the target file exists and is reachable, etc.).
so; use $_SERVER['DOCUMENT_ROOT']
As a side note, you do not need to use brackets for your includes/requires, it's simply giving the server more work to do for no extra benefit.
The $_SERVER['DOCUMENT_ROOT'] is the base directory of your PHP/web application, typically the contents of the folder /public_html.
Using correct syntax and the above $_SERVER value (which will point to the /public_html folder you will have:
require_once $_SERVER['DOCUMENT_ROOT'].'/PHPMailer/PHPMailerAutoload.php';
This will work from any script within your directory structure, if the file (PHPMailerAutoload.php) exists and is reachable at that given location
Given your location
/public_html/classes/filethatwantstoacces.php
doing ../ gives you
/public_html/classes
so ../PHPMailer/PHPMailerAutoload.php evaluates to
/public_html/classes/PHPMailer/PHPMailerAutoload.php
As #Martin has pointed out, using $_SERVER['DOCUMENT_ROOT'] to construct an absolute path to your file is the easiest way to avoid relative directory navigation errors such as this:
require_once $_SERVER['DOCUMENT_ROOT'].'/PHPMailer/PHPMailerAutoload.php';
I have a php file (php1.php). Within that php file i have the following line:
include('php/PROTECT/login.php')
However when i load the page i get the following error:
Warning: include(php/login.php) [function.include]: failed to open stream: No such file or directory
it just totally ignores the /PROTECT/ section?
Does anyone have any ideas why this is and how i can resolve the issue?
My file structure is as follows: (php1.php is within /php and login.php is within /php/PROTECT
It should look like:
include('php/PROTECT/login.php');
Make absolutely sure that that's the include call that's throwing the error. Make sure that no other code, in php1.php or in any file it has included or required tries to include php/login.php.
If php1.php is within /php/ (which I understand it to be), then you will need to use the following:
include('PROTECT/login.php');
Includes in includes can be a bit messy, but if you concatenate with DIR it is easier. DIR is always path to the file it self, if you use DIR you can always use the relative path. Like this:
include DIR . "/PROTECT/login.php";
As you can see by the error message:
Warning: include(php/login.php)
"PROTECT" is excluded from the path, probably for being in all caps.
Change the directory to all lowercase ("protect" or similar) and see if you get the same error.
i'm getting following error in my PHP file.
Warning: include(../config/config.php) [function.include]: failed to open stream: No
such file or directory in C:\xampp\htdocs\my-proj\functions\function.php on line 2
let me describe my folder structure
ROOT folder /index.php
functions / function.php
config / config.php
signup / signup.php
now, If i use absolute path, then it is give the same error in signup.php, and if I use relative path then it is giving this error in index.php
any help would be appreciated.
use
include("$_SERVER[DOCUMENT_ROOT]/config/config.php");
The file paths are relative to the invoked script. If your application gets invoked by http requests to index.php, then the include() path needs to be relative to that - even if the include statement itself is located in the functions.php script.
A common workaround is to make all paths absolute in relation to the document root:
include("$_SERVER[DOCUMENT_ROOT]/config/config.php");
// Note: leaving out array keys only valid in double quote string context.
That would work in index.php and functions.php alike.
Use include __DIR__."/../config/config.php"; if you want to include a file relative to the file you're currently executing in. If you're using a version of php older than 5.3.0 (you shouldn't), replace __DIR__ with dirname(__FILE__).
$_SERVER['DOCUMENT_ROOT'] is not set when using commandline and requires that your project is relative to the DOCUMENT_ROOT instead of allowing the user to place it wherever they please. If phpMyAdmin used this variable, you would be forced to accommodate it instead of just placing it wherever you want. That's another thing, it's a variable, so there's a potential security issue too.
If config.php is necessary, I suggest using require instead, otherwise use if (file_exists($file)) {require $file;} so you can avoid warnings when it doesn't exist and get an error when it can't be read (I assume if it exists, it's intended to be used).
I'm a total PHP noob and am using a pretty simple PHP include:
<?php include("~head.php"); ?>
to do a bit of templating for a website (to achieve common headers, footers, menus for all my pages).
NOTE: The tilde (~) is simply there to make the directories easier to look at (pushes main files to the top of the list when sorted alphabetically)
It's working great for files that are in the same directory but when I reference a file outside of a directory, like so:
<?php include("../~head.php"); ?>
However, it simply doesn't seem to be finding the file as the header is clearly not being pulled into the markup.
Conversely, if I reference the file with a full url, e.g.
<?php include("http://example.com/~head.php"); ?>
I get the following error code on my page.
Warning: include() [function.include]: URL file-access is disabled in the server configuration in /home/content/65/7392565/html/bikini/angela_bikini.php on line 1
Warning: include(http://example.com/~head.php) [function.include]: failed to open stream: no suitable wrapper could be found in /home/content/65/7392565/html/products/product_a.php on line 1
Warning: include() [function.include]: Failed opening 'http://example.com/~head.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /home/content/65/7392565/html/products/product_a.php on line 1
Strangely, the "../file.php" syntax works for non-header files (e.g. the include I'm using for the menu).
As such code's gotten to be a bit of a fragmented mess and is difficult to maintain changes across all the different pages. Any thoughts or solutions would be very much appreciated. I really am a noob tho so I probably won't be able to wrap my head around anything too fancy. : )
Thanks for your time.
Jon
Rather than using only the ../ to get the directory above, a construct like this will create the full filepath:
// Assuming you are including from the root
$application_path = dirname(__FILE__);
include("$application_path/../header.php);
Typically I'll do this by defining a constant, rather than using a variable.
define('APP_PATH', dirname(__FILE__));
Use this as:
// Assuming you are including at the file root:
define('APP_PATH', dirname(__FILE__));
include(APP_PATH . "/include/head.php");
// Assuming you are including from /include (one directory in)
// append a "/../" onto the end to indicate that the application
// root is one directory up from the currently executing file.
define('APP_PATH', dirname(__FILE__) . "/../");
include(APP_PATH . "somefile_at_the_root.php");
You have to be careful with the tilde! Under UNIX-like operating systems, the tilde is a shortcut to your home directory. If maybe the Apache server runs under the account www, your file-reference could be interpreted like this:
/home/www/head.php
And for the approach of using the full URL, the error says all:
URL file-access is disabled in the server configuration
Ignoring that it isn't best practice to use full URLs (because your folder structure could change etc.), you have to enable allow_url_include in your php.ini (see PHP.net).
If you really want to have your important files on top, you could use the underscore _.