2013-03-04 30 views
0

我試圖使用crontab運行腳本,但是即使從命令行我也遇到問題。該腳本在瀏覽器中運行良好,並在reports/文件夾中創建了一些excel文件,該文件夾歸apache所有。腳本文件歸我所有:john從命令行在PHP上拒絕的權限

當我嘗試從命令行運行腳本時,出現以下警告,並且不會創建excel文件。我嘗試了完整的路徑,但我得到了相同的結果。

這是我後,我嘗試從命令行

public_html]$ php include/tests/mailme.php 

PHP Warning: fopen(reports/4-March.xls): failed to open stream: Permission denied in /home/john/public_html/PHPExcel/Shared/OLE/PPS/Root.php on line 90 

Warning: fopen(reports/4-March.xls): failed to open stream: Permission denied in /home/john/public_html/PHPExcel/Shared/OLE/PPS/Root.php on line 90 
PHP Fatal error: Uncaught exception 'Exception' with message 'Can't open reports/4-March.xls. It may be in use or protected.' in /home/john/public_html/PHPExcel/Shared/OLE/PPS/Root.php:93 
Stack trace: 
#0 /home/john/public_html/PHPExcel/Writer/Excel5.php(233): PHPExcel_Shared_OLE_PPS_Root->save('reports/...') 
#1 /home/john/public_html/include/generate_daily_excel.inc(401): PHPExcel_Writer_Excel5->save('reports/...') 
#2 /home/john/public_html/include/tests/mailme.php(41): generateDailyExcel('04-02-2013') 
#3 {main} 
thrown in /home/john/public_html/PHPExcel/Shared/OLE/PPS/Root.php on line 93 

Fatal error: Uncaught exception 'Exception' with message 'Can't open reports/4-March.xls. It may be in use or protected.' in /home/john/public_html/PHPExcel/Shared/OLE/PPS/Root.php:93 
Stack trace: 
#0 /home/john/public_html/PHPExcel/Writer/Excel5.php(233): PHPExcel_Shared_OLE_PPS_Root->save('reports/...') 
#1 /home/john/public_html/include/generate_daily_excel.inc(401): PHPExcel_Writer_Excel5->save('reports/...') 
#2 /home/john/public_html/include/tests/mailme.php(41): generateDailyExcel('04-02-2013') 
#3 {main} 
    thrown in /home/john/public_html/PHPExcel/Shared/OLE/PPS/Root.php on line 93 

我認爲如果我改變的reports所有者,瀏覽器版本將不會被允許保存運行該腳本。我不熟悉Linux和權利。

回答

2

當您從命令行運行php代碼。腳本以您的用戶權限運行,而不是以www數據用戶權限運行。

您應該運行腳本作爲根(不推薦),或代替更改文件權限:

sudo chown yourUser:www-data file 
    sudo chmod 664 file 

而且你應該做的幾乎相同與目錄:

sudo chown yourUser:www-data dir 
    sudo chmod 775 dir 
+0

我不會特別推薦以root身份運行,避免以root身份運行任何東西,除非您必須 – 2013-03-04 19:40:48

+0

我同意Mark的意見。並編輯我的評論。 – Kovge 2013-03-05 09:19:42

1

的錯誤是非常明確地爲你安排:「無法打開流:權限被拒絕」。當你從命令行運行這個時,你是用戶「john」(我從你的主路徑中假設)。用戶「john」是否有權寫入報告目錄?

實際報告目錄的權限是什麼?

ls -al /home/john/public_html/reports