2015-06-17 148 views
0

我在Ubuntu 14.04上用LAMP制作了简单的http服务器,并将我的html,php和js文件放到了Apache根目录/var/www/。这些文件中的所有路径都是绝对路径,看起来像/script1.php/subdir/script.php。 Web服务器的目录树是这个样子:从服务器根目录重定向到apache根目录

/ 
|---var 
    |---www 
     |---script1.php 
     |---subdir 
|---usr 
|---home 
|---etc 

文件/etc/apache2/sites-available/000-default.conf

ServerAdmin [email protected] 
    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认为'/'意味着不是根网站服务器目录,而是根目录apache?

+0

我不认为这是可能,更好地利用变量存储的document_root绝对路径,然后用实际项目串接文件夹 // $ site_root ='/ vat/www /'; opendir($ site_root.'subdir');或者看看这里http://www.php.net/manual/en/language.constants.predefined.php –

+1

或使用$ _SERVER ['DOCUMENT_ROOT']; http://php.net/manual/en/reserved.variables.server.php –

+0

谢谢你的答案。我会试试 – Andrew

回答

0

opendir采用实际路径而不是Web服务器文档根目录的路径。因此,opendir(“/”)正常工作以向您显示服务器的根目录。

正如普京所说,宁愿用

opendir($_SERVER["DOCUMENT_ROOT"]); 

或可能更好的安全

opendir(filter_var($_SERVER["DOCUMENT_ROOT"], FILTER_INPUT_STRING));