I am trying to access my navigation.php file so I can include it in my login.php file. Like so:
<?php
include "../navigation.php";
?>
but the navigation only displays without css styles. For some reason this ONLY happens with I add a folder within the root folder of the project I am working on with php files. How do I access the navigation php file from a child folder?
Here is a screenshot of my file folder:
Related
I've a web site made of some folders, one for each section (info, news, blog etc...).
In each of these folders there is an index.php file that should load a layout (common to all). These are stored in a different folder in the root where there is also the main index.php file (the homepage). So i have something like this:
root
-index.php (home)
-/layout
--layout files
-/info
--index.php`
The index file in the /info folder should include the page layout from /layout.
The problem is that the layout files should include other files from other folders.
In layout files I put this:
include 'contents/page-element.php';
But if I try to reach the same page-element.php file from the index.php file in the /info folder, I should do:
include '../contents/page-element.php';
to go to the root and then reach the /layout folder.
I don't want to create a copy of the layout for the folders so I've tried $_SERVER['DOCUMENT_ROOT'], but it does not work in some cases on the localhost and even on the web server.
Can someone help me or let me know how I can build a dynamic absolute path?
By using the built-in constant __DIR__ you can get your absolute path
by print it or save
echo __DIR__;
it will print something like this for you
C:\Users\user\Desktop\test
and there is also another constant that will get the absolute path for your file that executes the command __FILE__
echo __FILE__;
will give you a result like this
C:\Users\user\Desktop\test\index.php
I have create my system of three folders
pages folder
style folder
images folder
and one index.php file.
My problem is how I can include index.php file in my file login.php which are found in pages folder.
I'm trying to header ("Location:index.php") don't work.
Not: this index.php is single file located outside of those folders
<?php
header ("Location:index.php");
?>
Just make the URL's relative from the document root (by adding an initial /):
header('Location: /index.php');
or if you want to redirect to a sub folder:
header('Location: /theFolder/index.php');
Then you don't need to care what folder you're in and add a bunch of ../../.
When traversing a folder tree, you can use ../ to denote a parent folder.
. is a shortcut to 'the current folder'.
.. is a shortcut to 'the folder containing the current folder'.
Assuming the index.php file is in the parent folder of the login.php page, you can access it like so:
header ("Location: '../index.php'");
Ive looked everywhere but cant find anything.
I have a few php folders in a folder in htdocs. Heres the layout:
htdocs(root folder)>widget_corp>
public>(php file i need to include header.php in)
includes>layouts>header.php
I have tried to include header.php in the php file in public but using the include statement but it hasnt worked. What should i put in my inlcude statement to include the header.php?
If your folder structure is the following:
public -> example.php
includes -> layouts -> header.php
When opening example.php, you can use the code:
include "../includes/layours/header.php";
And that will include the PHP header.php file into your example.php file.
Note: The ../ means going to the Parent Directory, and the ./ means current directory, just wanted to add that for you in the future.
I've designed header in Adobe Muse and I want to include it on my wordpress theme. I tried to use a php include statement like so within the header.php file:
<?php
chdir('pages');
set_include_path('pages');
include 'index.html';
?>
The code calls the correct file but none of the images or css is loading. When I look at the debugging console it shows that the code is not calling the correct path for these images and css. It's looking in the root directory instead of the pages directory. How can I get the index.html file to run with the correct working directory?
I am trying to clean up my website folder structure. I have too many processing scripts in the root directory. The reason is because they won't work if I move them to a sub folder and call them from there.
For eg.
root directory
- images folder
- css folder
- js folder
- includes folder
- admin folder
index.php
ajax.php
Say I move the ajax.php to includes folder, it will look like this.
root directory
- images folder
- css folder
- js folder
- includes folder
- ajax.php
- core folder
- template folder
index.php
Now on every page I have an init.php required. In ajax.php for eg.
<?php require_once 'core/init.php'; ?>
// rest of the code
The issue I am having is that the init.php file won't run as long as ajax.php is in includes folder. I get an error something like this.
Warning: require_once(core/init.php): failed to open stream:
If i move the ajax.php back to the root directory, it'll work fine.
On index.php, this is how I am calling the ajax.php
core/init.php works fine when requiring in the "templates" folder.
Perhaps someone can tell me the solution to this?
Try using PHP's chdir function. This will change the working directory that your PHP environment is working in, and therefore the relative paths that your include functions are looking in. Ideally, you'd want to call chdir with the root of your project, so that when you do require_once('core/init.php'); you don't end up calling from the core directory, but rather your . directory.