Search code examples
phprequire-oncephp-include

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.


Solution

  • 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