2014-03-24 63 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"