2016-08-30 95 views
0

脚本文件名:更改PHP的当前目录

c:\wtserver\www\testclass\index.php 
http://localhost = c:\wtserver\www\ 

代码:

<?php 
     print getcwd()."<br/>"; 
     chdir(dirname(__FILE__).'\product\\'); 
     print getcwd()."<br/>"; 
     print '<img src="img/1.jpg"/>'; 
?> 

输出是:

c:\wtserver\www\testclass 
C:\WTServer\WWW\testclass\product 

和图像的位置是:

http://localhost/testclass/img/1.jpg 

我需要的是,图像位置应该是:

http://localhost/testclass/product/img/1.jpg 

不改变这一行:

print '<img src="img/1.jpg"/>'; 

感谢。

+0

为什么你不想改变那条线? 'src'中的相对路径对php不起任何作用,但对于html解析客户端:客户端试图相对于包含文档的位置发出get请求。 – deeenes

回答

2

更换print '<img src="img/1.jpg"/>';与打印'<img src="product/img/1.jpg"/>';

chdir和建筑URL之间没有相关性。 chdir命令仅对服务器/本地计算机的文件系统的文件夹有影响,并且它不参与形成页面的URL。

+0

也许可以推荐'',如果他想全球化 – bwoebi

+0

谢谢@bwoebi,那就是我所需要的。对不起,我是新来的HTML和PHP。 – alnaaaa