2013-03-04 31 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