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

后端开发   发布日期:2023年05月12日   浏览次数:444

最近在使用阿里云主机的时候,发现用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);

            }

        }

    }

}

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

以上就是关于使用阿里云主机报错“realpath() [function.realpath]: open_basedir restriction in effect”的解决办法的详细内容,更多关于关于使用阿里云主机报错“realpath() [function.realpath]: open_basedir restriction in effect”的解决办法的资料请关注九品源码其它相关文章!