欢迎您来到,李雷博客 | PHP博客        登录  |  注册

关于使用阿里云主机报错“realpath() [function.realpath]: open_basedir restriction in effect”的解决办法

更新:2018-08-16 10:11:02
人气:4432
来源:本站整理
A+

最近在使用阿里云主机的时候,发现用PHPEXCEL导出数据表时,系统报出以下错误:

Warning: realpath() [function.realpath]: open_basedir restriction in effect. File(/tmp) is not within the allowed path(s): (/data/home/byu2599880001/:/usr/home/byu2599880001/:/data/home/tmp/:/usr/home/tmp/:/var/www/disablesite/) in /data/home/byu2599880001/htdocs/peixun/PHPExcel/Shared/File.php on line 136

在网上查询,有些是让修改PHP.INI文件,但一般用主机类的用户都没有权限进行设置,后来在网上查询,是可以通过修改PHPExcel/Shared/File.php文件进行解决。

查找到以下代码函数function sys_get_temp_dir()

public static function sys_get_temp_dir()

{

    // sys_get_temp_dir is only available since PHP 5.2.1
    // http://php.net/manual/en/function.sys-get-temp-dir.php#94119

    if (!function_exists('sys_get_temp_dir')) {

        if ($temp = getenv('TMP')) {

            if (file_exists($temp)) {
                return realpath($temp);
            }

        }

        if ($temp = getenv('TEMP')) {

            if (file_exists($temp)) {
                return realpath($temp);
            }

        }

        if ($temp = getenv('TMPDIR')) {

            if (file_exists($temp)) {
                return realpath($temp);
            }

        }

        // trick for creating a file in system's temporary dir
        // without knowing the path of the system's temporary dir
        $temp = tempnam(__FILE__, '');

        if (file_exists($temp)) {

            unlink($temp);

            return realpath(dirname($temp));

        }

        return null;

    }

    // use ordinary built-in PHP function
    //There should be no problem with the 5.2.4 Suhosin realpath() bug, because this line should only
    //be called if we're running 5.2.1 or earlier
    return realpath(sys_get_temp_dir());

}

将以上代码替换为以下代码:

public static function sys_get_temp_dir()

{

    // use upload-directory when defined to make it running on
    // environments having very restricted open_basedir configs
    if (ini_get('upload_tmp_dir') !== false) {

        if ($temp = ini_get('upload_tmp_dir')) {

            if (file_exists($temp)) {
                return realpath($temp);
            }

        }

    }

    // sys_get_temp_dir is only available since PHP 5.2.1
    // http://php.net/manual/en/function.sys-get-temp-dir.php#94119

    if (!function_exists('sys_get_temp_dir')) {

        if ($temp = getenv('TMP')) {

            if (file_exists($temp)) {

                return realpath($temp);

            }

            if (($temp != '') && file_exists($temp)) {

                return realpath($temp);

            }

        }

        if ($temp = getenv('TEMP')) {

            if (file_exists($temp)) {

                return realpath($temp);

            }

        }

    }

}

只需要将函数里的程序代码替换一下即可,函数名不用动。替换后问题解决。

推荐的文章
# 发表我的评论
  /     /  
# 最近评论

由于目标计算机积极拒绝,无法连接

  Ads by Google
  联系博主
Hello,本博客系统采用PHP和MySql开发,程序开发完全是因为个人爱好,是自己纯手写PHP源代码,未采用任何PHP框架!
QQ:858353007   微信号:lileihot123
网站地图
会员服务
关于我们
QQ:858353007
 
广告服务
加我微信
移动端访问
 
 
Copyright © 2014- 2024 www.mdaima.com All Rights Reserved.
李雷博客,专注PHP经验、PHP教程及PHP源代码开源下载分享的PHP博客!   ICP备案号:京ICP备10202169号-4