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

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

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

  1. 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()

  1. public static function sys_get_temp_dir()
  2.  
  3. {
  4.  
  5. // sys_get_temp_dir is only available since PHP 5.2.1
  6. // http://php.net/manual/en/function.sys-get-temp-dir.php#94119
  7.  
  8. if (!function_exists('sys_get_temp_dir')) {
  9.  
  10. if ($temp = getenv('TMP')) {
  11.  
  12. if (file_exists($temp)) {
  13. return realpath($temp);
  14. }
  15.  
  16. }
  17.  
  18. if ($temp = getenv('TEMP')) {
  19.  
  20. if (file_exists($temp)) {
  21. return realpath($temp);
  22. }
  23.  
  24. }
  25.  
  26. if ($temp = getenv('TMPDIR')) {
  27.  
  28. if (file_exists($temp)) {
  29. return realpath($temp);
  30. }
  31.  
  32. }
  33.  
  34. // trick for creating a file in system's temporary dir
  35. // without knowing the path of the system's temporary dir
  36. $temp = tempnam(__FILE__, '');
  37.  
  38. if (file_exists($temp)) {
  39.  
  40. unlink($temp);
  41.  
  42. return realpath(dirname($temp));
  43.  
  44. }
  45.  
  46. return null;
  47.  
  48. }
  49.  
  50. // use ordinary built-in PHP function
  51. //There should be no problem with the 5.2.4 Suhosin realpath() bug, because this line should only
  52. //be called if we're running 5.2.1 or earlier
  53. return realpath(sys_get_temp_dir());
  54.  
  55. }

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

  1. public static function sys_get_temp_dir()
  2.  
  3. {
  4.  
  5. // use upload-directory when defined to make it running on
  6. // environments having very restricted open_basedir configs
  7. if (ini_get('upload_tmp_dir') !== false) {
  8.  
  9. if ($temp = ini_get('upload_tmp_dir')) {
  10.  
  11. if (file_exists($temp)) {
  12. return realpath($temp);
  13. }
  14.  
  15. }
  16.  
  17. }
  18.  
  19. // sys_get_temp_dir is only available since PHP 5.2.1
  20. // http://php.net/manual/en/function.sys-get-temp-dir.php#94119
  21.  
  22. if (!function_exists('sys_get_temp_dir')) {
  23.  
  24. if ($temp = getenv('TMP')) {
  25.  
  26. if (file_exists($temp)) {
  27.  
  28. return realpath($temp);
  29.  
  30. }
  31.  
  32. if (($temp != '') && file_exists($temp)) {
  33.  
  34. return realpath($temp);
  35.  
  36. }
  37.  
  38. }
  39.  
  40. if ($temp = getenv('TEMP')) {
  41.  
  42. if (file_exists($temp)) {
  43.  
  44. return realpath($temp);
  45.  
  46. }
  47.  
  48. }
  49.  
  50. }
  51.  
  52. }

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

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