0

我正在尝试使用 crontab 运行脚本,但即使从命令行也无法使其工作。该脚本在浏览器中运行良好,并reports/apache. 脚本文件归我所有: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 和权利的新手。

4

2 回答 2

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
于 2013-03-04T08:59:37.847 回答
1

该错误非常明确地为您列出:“无法打开流:权限被拒绝”。当您从命令行运行它时,您是用户“john”(我假设来自您的主路径)。用户“john”是否有权写入报告目录?

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

ls -al /home/john/public_html/reports
于 2013-03-04T09:00:18.807 回答