I have included a PHP file on my mainpage, inside the php file there's another include of a .js file, the included php file is in a different directory than the mainpage, so the included .js file can't be found.
To visualize:
Mainpage - Root
Included PHP file -File A located in Folder A
Included JS file in File A is located in Folder A
But it's looking for the .js file in Root instead of Folder A
How do I get the path right ?
Also it should be dynamic, I might not know the actual name of Folder A
Use the .. notation to traverse folders
the path would be '../script.js'
Site:
www.yoursite.com
Folders:
/index.php
/lib/header.php
/contacts/index.php
/js/main.js
Let's say you want to inclue your /js/main.js file inside /lib/header.php in order to work wherever you include /lib/header.php, for example in /index.php and /contacts/index.php.
You would have the following inside /lib/header.php :
<script src="http://www.yoursite.com/js/main.js" type="text/javascript"></script>
if include.php and xyz.js are in the same directory. than
include .js file in include.php like that--
<script type="text/javascript" language="javascript" src="xyz.js"></script>
if it is not working than u should use Relative Url/SITE URL like that--
<script type="text/javascript" language="javascript" src="http://localhost/folder/xyz.js"></script>
Related
I am new to php and facing initial problems :).
My directory structure is as follows :
site
index
homepagesections.php
assets
css
style.css
config
db.php
include
header.php
index.php
In the index.php , which is in the root folder I need to include the header.php file which includes config/db.php and also assets/css/style.css.
I have tried all the options ( including. the DIR option ) but failed. What is the best way to include these files and also the path for the style.css ?
In index.php path to header is include/header.php:
include('include/header.php');
In header your path to db.php is ../config/db.php so you will have:
include('../config/db.php');
Note '../' - this is how you navigate 1 level up, from the directory your file is. In header adding css will be something like:
<link rel="stylesheet" href="../assets/css/style.css">
In index.php:
include (__DIR__.'/include/header.php');
In header.php:
include (__DIR__.'/../config/db.php');
include (__DIR__.'/../assets/css/style.css');
I have an index.php in root and a 'includes' folder containing header.php and footer.php.I included header and footer in index.php and it works fine but in header and footer I include some different files in config folder in root.
I know that I should use "../config/config.php" but it works with 'config/config.php'.
I included header and footer in index.php and I should use path according to the index that I included to ???
what if I include one file in several folders ???
Try to use __DIR__ it will give you the path compared to where your current file exist, also try to use include_once instead of include
DIR : The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to
dirname(FILE). This directory name does not have a trailing slash
unless it is the root directory.
Include your file like this from within your header.php and footer.php:
include_once DIR.'/../config/config.php';
do the same technique for other files
Yes your path should be according to index.php that's mean your root directory, not your includes folder.
At any time you can check such issues by
echo __DIR__;
to make sure from your path.
I have directory structure like this:
- index.php
- templates(directory)
- login.php
- header.php
- js(directory)
- jquery.min.js
I include script jquery.min.js in header.php, then I include header.php in login.php, but the path to jquery.min.js brokes. How I try to solve it by replace this:
<script src="js/jquery.min.js">
to this
<script src="<?php echo dirname(__FILE__)?>js/jquery.min.js">
but then I get this path to resourse:
http://localhost/var/www/auth/js/jquery.min.js
And again 404 error. Can somebody explain to me how I can solve this issue.
js's script files work in context of web-root. as long as your web-root is where your index.php is then <script src="/js/jquery.min.js"> should work
You use <script src="js/jquery.min.js"> which is a relative path. so, if you call login.php browser requests js relatively to it: templates/js/jquery.min.js which doesn't exist, obviously
What you're trying to do is find the path of the javascript file relative to the root of your application, rather than any specific php file. So, if you change the script tag to begin with a /, it'll serve from the root of your application, regardless of where the php files are.
Changing it to <script src="/js/jquery.min.js"> will serve it from http://localhost/js/jquery.min.js regardless of where in your structure your php files are.
So I have some php files that I'm trying to include with no luck.
I have the root directory, and off that I have a directory called aboutus. In the aboutus directory, I'm trying to include a css file from the root. So I use this code:
include $_SERVER['DOCUMENT_ROOT'] . "/meta-head.php";
The include works fine, but in the meta-head.php there are some calls to files, one of which is:
<script src="_assets/js/navbar.js"></script>
The problem is that it's trying to access this from root/aboutus/_assets/js/navbar.js (which doesn't exist), when I want it to acccess it at root/_assets/js/navbar.js.
What am I doing wrong so that it won't access the file relative to the root?
Thank you!
use absolute path or use this:
<script src="/_assets/js/navbar.js"></script>
I am trying to include a .js file into a php file.
My folder structure looks like this:
root
---js (FOLDER)
------file.js
---blog (FOLDER)
------index.php
------js (FOLDER)
---------blog.js
If I am using this:
<script type="text/javascript" src="../js/blog.js"></script>
it works just fine.
What I can't seem to do is include file.js from under the root's "js" directory.
I've tried everything I can think of but I just can't seem to make it work.
<script type="text/javascript" src="/js/file.js"></script>
would reference the javascript file in the top level js directory
To access the blog, is a user going to www.domain.com or www.domain.com/blog?
If /root/blog is your DOCROOT in your web server, the /root/js directly likely isn't web-accessible. The location you're referencing MUST be accessible to the client's browser.
An easy way to test this is to enter the path to the JS file directly into your browser.
it looks like blog is in a subdirectory of the folder index.php is in. Discard the ../ bit.
<script type="text/javascript" src="js/blog.js"></script>
<script type="text/javascript" src="../js/file.js"></script>