2014-03-24 61 views
0

我有以下的代码应该在文件服务器中保存:PHPExcel保存动态档案

$month_name = "MARCH2014"; 


     $objWriter->save(str_replace(__FILE__,"C:\xampp\htdocs\timesheet\files\{$month_name}\{$username}.xlsx",__FILE__)); 

我不断收到以下错误:

Fatal error: Uncaught exception 'PHPExcel_Writer_Exception' with message 'Could not close zip file C: mpp\htdocs imesheetiles\{MARCH2014}\{HS0103}.xlsx.' in C:\xampp\htdocs\timesheet\application\third_party\PHPExcel\Writer\Excel2007.php:399 Stack trace: #0 C:\xampp\htdocs\timesheet\application\controllers\time_sheet.php(6132): PHPExcel_Writer_Excel2007->save('C:?mpp\htdocs?i...') #1 [internal function]: Time_sheet->save_time_sheet() #2 C:\xampp\htdocs\timesheet\system\core\CodeIgniter.php(359): call_user_func_array(Array, Array) #3 C:\xampp\htdocs\timesheet\index.php(202): require_once('C:\xampp\htdocs...') #4 {main} thrown in C:\xampp\htdocs\timesheet\application\third_party\PHPExcel\Writer\Excel2007.php on line 399 

但是当我用下面的:

$objWriter->save(str_replace(__FILE__,"C:\xampp\htdocs\timesheet\files\MARCH2014\HS0103.xlsx",__FILE__)); 

我得到没有错误, 请告知我做错了什么?

+0

你确定你的更换是必要的吗?为什么不使用该字符串作为您的替代品替代所有__FILE__呢? – ToBe

+0

最佳想法allways正在调试!在使用它之前输出文件路径到 - > save()并检查它是否包含你认为它的作用。 – ToBe

回答

0

标准PHP转义字符:

"C:\xampp\htdocs\timesheet\files\{$month_name}\{$username}.xlsx" 

企图逃跑

\xa => character <hex 0a> (<Line feed>) 
\h => ??? 
\t => <tab character> 
\f => ??? 

如果你想有一个\\而非PHP转义序列的第一个字符,那么你需要将其转义为\\

因此

"C:\\xampp\\htdocs\\timesheet\\files\\{$month_name}\\{$username}.xlsx" 

另一种方法是使你的目录路径“安全”使用UNIX目录分隔符(/),这将仍然也可以在Windows和Mac电脑

"/xampp/htdocs/timesheet/files/{$month_name}/{$username}.xlsx"