dreamer1231
2015-01-12 09:51
浏览 83
已采纳

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.

图片转代码服务由CSDN问答提供 功能建议

我有这样的目录结构:

   -  index。  php 
  -  templates(目录)
  -  login.php 
  -  header.php 
  -  js(目录)
  -  jquery.min.js 
   
 
 <  p>我在 header.php 中包含脚本 jquery.min.js ,然后在 login.php <中包含 header.php   / code>,但 jquery.min.js 的路径是破坏的。 我如何通过替换它来尝试解决它: 
 
 

&lt; script src =“js / jquery.min.js”&gt;

&lt; script src =“&lt;?php echo dirname(__ FILE __)?&gt; js / jquery.min.js”&gt; < / code> 然后我得到了这条资源的路径:

  http://localhost/var/www/auth/js/jquery.min.js 
   
 
 

再次404错误。 有人可以向我解释如何解决这个问题。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doushan7997 2015-01-12 09:56
    已采纳

    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

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongyi3616 2015-01-12 09:56

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题