从命令行对php的权限被拒绝

时间:2013-03-04 08:52:48

标签: php phpexcel

我正在尝试使用crontab运行脚本,但即使从命令行运行它也有问题。该脚本可以在浏览器中正常运行,并在reports/文件夹中创建一些由apache拥有的excel文件。脚本文件归我所有:john

当我尝试从命令行运行脚本时,我收到以下警告,并且不会创建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 个答案:

答案 0 :(得分:2)

从命令行运行php代码时。该脚本使用您的用户权限运行,而不是使用www-data用户权限运行。

您应该以root身份运行脚本(不推荐),或者更改文件权限:

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

你应该对目录做几乎一样的事情:

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

答案 1 :(得分:1)

错误的描述非常明确:“未能打开流:权限被拒绝”。当您从命令行运行它时,您是用户“john”(我假设从您的主路径)。用户“john”是否有权在报告目录中写入?

实际报告目录的权限是什么?

ls -al /home/john/public_html/reports