open_basedir造成文件上传失败

[ 2010-12-27 14:23:07 | 作者: admin ]
字号: | |
Warning: file_exists() [function.file-exists]: open_basedir restriction in effect. File(/home) is not within the allowed path(s): (/home/ftp/512j.com/d/u/c/duchun66/:/tmp/)

于是我在网上搜索一下,发现是open-basedir设置的问题,搜索到文章如下:

******************文章1*********************************************************************************

open_basedir: 将用户可操作的文件限制在某目录下;
--------------------------------------------------------------------------------
如下是php.ini中的原文说明以及默认配置:
; open_basedir, if set, limits all file operations to the defined directory
; and below. This directive makes most sense if used in a per-directory or
; per-virtualhost web server configuration file. This directive is
; *NOT* affected by whether Safe Mode is turned On or Off.
open_basedir = .
open_basedir可将用户访问文件的活动范围限制在指定的区域,通常是其家目录的路径,也
可用符号"."来代表当前目录。注意用open_basedir指定的限制实际上是前缀,而不是目录名。
举例来说: 若"open_basedir = /dir/user", 那么目录 "/dir/user" 和 "/dir/user1"都是
可以访问的。所以如果要将访问限制在仅为指定的目录,请用斜线结束路径名。例如设置成:
"open_basedir = /dir/user/"

open_basedir也可以同时设置多个目录, 在Windows中用分号分隔目录,在任何其它系统中用
冒号分隔目录。当其作用于Apache模块时,父目录中的open_basedir路径自动被继承。

有三种方法可以在Apache中为指定的用户做独立的设置:

(a) 在Apache的httpd.conf中Directory的相应设置方法:

          php_admin_value open_basedir /usr/local/apache/htdocs/
#设置多个目录可以参考如下:
          php_admin_value open_basedir /usr/local/apache/htdocs/:/tmp/


(b) 在Apache的httpd.conf中VirtualHost的相应设置方法:
php_admin_value open_basedir /usr/local/apache/htdocs/
#设置多个目录可以参考如下:
php_admin_value open_basedir /var/www/html/:/var/tmp/

(c) 因为VirtualHost中设置了open_basedir之后, 这个虚拟用户就不会再自动继承php.ini
中的open_basedir设置值了,这就难以达到灵活的配置措施, 所以建议您不要在VirtualHost
中设置此项限制. 例如,可以在php.ini中设置open_basedir = .:/tmp/, 这个设置表示允许
访问当前目录(即PHP脚本文件所在之目录)和/tmp/目录.

请注意: 若在php.ini所设置的上传文件临时目录为/tmp/, 那么设置open_basedir时就必须
包含/tmp/,否则会导致上传失败. 新版php则会提示"open_basedir restriction in effect"
警告信息, 但move_uploaded_file()函数仍然可以成功取出/tmp/目录下的上传文件,不知道
这是漏洞还是新功能.

针对ShopEx472版本的配置:

open_basedir = "D:/Server/wwwroot;../catalog;../include;../../home;../syssite;../templates;../language;../../language;../../../language;../../../../language"

 

***********************文章2:***************************************************************************

首先,我写的程序在本地运行的好好的,刚才传到空间里运行,报错:

open_basedir restriction in effect. File(/) is not within the allowed path(s):******

会影响 realpath,is_dir,is_file等文件及目录操作函数。

查了一下:

把php的文件操作限制在web目录里面。以下是修改apache配置文件httpd.conf的一个例子:

        php_admin_value open_basedir /usr/local/apache/htdocs
 

重启apache后,/usr/local/apache/htdocs目录下的PHP脚本就只能操作它自己目录下的文件了,否则PHP就会报错:
Warning: open_basedir restriction in effect.
File is in wrong directory in xxx on line xx.


在看我以前写的程序的片断:

  public static function mkpath($path,$r = 0777){
    $dirs=array();
    $dirs=explode("/",$path);
    $path="";
    foreach ($dirs as $element){
      $path.=$element."/";
      if(!is_dir($path)){
        if(!mkdir($path,$r)){
          return false;
        }
      } }
    return true;
  }其中,$dirs 的内容大概如下:var,data_disk,www,virtual,***,test,htdocs,test在foreach 里,有个 is_dir,当这个函数作用于 htdocs的上级 及 祖仙(。。。)目录时,因为open_basedir的因原,均会报错。。。。木有办法,谁叫咋只能用虚拟主机呢!只好修改函数啦! public static function mkpath($path,$r = 0777){ $path = str_replace(PATH_ROOT_ABS,'',$path); $dirs=array(); $dirs=explode("/",$path); $path= PATH_ROOT_ABS."/"; foreach ($dirs as $element){ $path.=$element."/"; if(!is_dir($path)){ if(!mkdir($path,$r)){ return false; } } } return true; }先去掉,在接上,这算咋回事啊。晕啊。。。


原文:http://chenling1018.blog.163.com/blog/static/148025420087250851438/
[最后修改由 admin, 于 2010-12-27 14:24:12]
评论Feed 评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=1621

这篇日志没有评论。

此日志不可发表评论。