假如有web目录如下:

index.html
class/ajax.js
class/a.php
class/b.php
img/img.jpg

index使用了class中的ajax.js调用class/a.php

那么,如果a.php需要用require_once调用b.php,应该怎么写呢?

require_once("b.php");

那么,如果a.php中打算用echo的方法调用现实img目录下的img.jpg要用什么路径呢?

答案是

echo "<img src="img/img.jpg" />";

这其中对“自己”的定位要分清楚

在php调用的时候,你站在的是后台的当前位置,而如果用echo之类,就是站到前台客户端的角度 -- 比如本例子中,ajax的话,就是作为index.html 的一部分显示,你的目录就是在 / 目录下了

总之,你在什么地方,根据的是你是在前台还是后台。

不仅php如此,在写jsp之类的也常常看到这样的问题。

开始老是目录,地位,角度看得傻掉了

用前后台分类后就可以很清晰的看到它的逻辑了

Categories: Code

Yu

Ideals are like the stars: we never reach them, but like the mariners of the sea, we chart our course by them.

1 Comment

ghostry · February 4, 2013 at 10:43

Google Chrome 24.0.1312.56 Google Chrome 24.0.1312.56 Windows 7 Windows 7

我总是分不清.所以都放到根目录.

Leave a Reply

Your email address will not be published. Required fields are marked *