我已经用LAMP在Ubuntu 14.04上制作了简单的http服务器,并将我的html,php和js文件放到apache根目录/var/www/。这些文件中的所有路径都是绝对的,看起来像 /script1.php 或 /subdir/script.php。Web 服务器的目录树如下所示:
/
|---var
|---www
|---script1.php
|---subdir
|---usr
|---home
|---etc
文件/etc/apache2/sites-available/000-default.conf:
ServerAdmin webmaster@localhost
DocumentRoot /var/www
<Directory />
Options Indexes FollowSymLinks
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
Alias /data /var/www/
<Location /data>
DAV On
AuthType Digest
AuthName "webdav"
AuthUserFile /usr/local/apache2/.digest_passwd.dav
Require valid-user
Order Allow,Deny
Allow from all
DirectoryIndex disabled
</Location>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
问题在于所有路径都从根服务器目录开始。
例如,php 命令 opendir('/') 显示列表:
/
|---var
|---usr
|---home
|---etc
但我需要这样的清单
/
|---subdir
|---script1.php
那么,我怎样才能让 Apache 认为"/"不意味着根 Web 服务器目录,而是根 apache 目录呢?
opendir 采用实际路径,而不是 Web 服务器文档根目录的路径。因此,opendir("/") 可以正常工作,以显示服务器的根目录。
正如弗拉基米尔所提到的,而是使用
opendir($_SERVER["DOCUMENT_ROOT"]);
或者可能更好的安全性
opendir(filter_var($_SERVER["DOCUMENT_ROOT"], FILTER_INPUT_STRING));