今日买了一个万网M3主机,发现PHPExcel无法正常导出xls文件,只能导出xlsx文件,导出的xls打开提示文件损坏,我还是坚持打开了这个文件,在文件里全是乱码,同时包含了以下错误代码:
<b>Warning</b>: realpath() [<a href='function.realpath'>function.realpath</a>]: open_basedir restriction in effect. File(/tmp) is not within the allowed path(s): (/data/home/hmu089229:/usr/home/hmu089229:/data/home/tmp:/usr/home/tmp:/var/www/disablesite) in <b>/data/home/hmu089229/htdocs/Classes/PHPExcel/Shared/File.php</b> on line <b>136</b><br />
通过上网查资料,发现是万网php.ini设置的问题,但是我又没有设置php.ini的权限,不过还是找到了解决办法,只需要修改Classes /PHPExcel/Shared/File.php这个文件即可。
搜索如下代码(就是这个文件中的最后一个函数):
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);
}
}
}
}
OK,问题解决。