nowiko
nowiko

Reputation: 2557

Path troubles when require or include file in PHP

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.

Upvotes: 0

Views: 166

Answers (2)

ConroyP
ConroyP

Reputation: 41896

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.

Upvotes: 0

JimiDini
JimiDini

Reputation: 2069

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

Upvotes: 1

Related Questions