dpn68721 2015-04-03 23:26
浏览 199

从命令行和crontab执行php时权限被拒绝

I am attempting to create a cronjob under the user apache, but I get permission denied errors for files that are accessed by the program. The specific file that my php script cannot access is /var/www/html/amazon/amazon_data.txt. Here is me checking the permissions, and testing to see if I can write to the file:

bash-3.2$ whoami
apache
bash-3.2$ ls -l /var/www/html/amazon/amazon_data.txt
-rwxrwxr-- 1 apache apache 1082 Apr  3 15:43 /var/www/html/amazon/amazon_data.txt
bash-3.2$ vi /var/www/html/amazon/amazon_data.txt

Now I try to run a script that tries to access the file I get this warning:

bash-3.2$ /usr/bin/php /var/www/html/amazon/amazon_inventory_sync.php
PHP Warning:  Module 'json' already loaded in Unknown on line 0
PHP Warning:  fopen(amazon_data.txt): failed to open stream: Permission denied in /var/www/html/amazon/amazon_inventory_sync.php on line 26

Warning: fopen(amazon_data.txt): failed to open stream: Permission denied in /var/www/html/amazon/amazon_inventory_sync.php on line 26
Unable to open amazon_data.txt!bash-3.2$

Why can I access and edit the file with the user just fine, but not in the php script when executing it via command line? There is no issue when I run the script from a browser.

Edit: I can run it fine under the user soh, who is in the group apache. apache is also in the group apache.

  • 写回答

1条回答 默认 最新

  • doubaolan2842 2015-04-04 03:12
    关注

    The issue was due to the file being requested not being a literal location. This is the code which did not work:

    $filename = "amazon_data.txt";
    $file = fopen($filename, "a+") or die("Unable to open $filename!
    ");
    

    This may work fine when the script is run remotely via HTTP, but it may cause issues when running cron jobs or execution in the terminal. Changing the $filename to be the full location fixed this issue.

    $filename = dirname(__FILE__)."/"."amazon_data.txt";
    $file = fopen($filename, "a+") or die("Unable to open $filename!
    ");
    

    Why does this happen? I am guessing instead of the php file's folder being used, it was using a directory which the apache user did not have access to, such as the current working directory that the command in terminal was being executed from.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀