【问题标题】:PHP - Getting the "failed to open stream: Permission denied " error after setting ownershipPHP - 设置所有权后出现“无法打开流:权限被拒绝”错误
【发布时间】:2019-09-03 10:14:12
【问题描述】:

我正在尝试使用 pear 的邮件类,但我的脚本没有权限访问我安装它的主文件夹中的 Mail.php,并且我已经正确地对它需要访问的文件执行了 chown 命令,这是我的 php 脚本的样子:

<?php

require_once "/home/ec2-user/pear/share/pear/Mail.php";

if (isset($_GET['trade']))
{

我得到的错误是:

Warning:  require_once(/home/ec2-user/pear/share/pear/Mail.php): failed to open stream: Permission denied in /var/www/html/newslookup/email.php on line 5
Fatal error:  require_once(): Failed opening required '/home/ec2-user/pear/share/pear/Mail.php' (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/html/newslookup/email.php on line 5

这里是为了表明我已经安装了 pear 并且我已经正确执行了 chown 命令:

[ec2-user@ip-172-31-90-218 ~]$ pwd
/home/ec2-user

[ec2-user@ip-172-31-90-218 ~]$ sudo chown -R ec2-user:apache pear   // here 

[ec2-user@ip-172-31-90-218 ~]$ ls -alrt
total 32
-rw-r--r--  1 ec2-user ec2-user  231 Jul 27  2018 .bashrc
-rw-r--r--  1 ec2-user ec2-user  193 Jul 27  2018 .bash_profile
-rw-r--r--  1 ec2-user ec2-user   18 Jul 27  2018 .bash_logout
drwxr-xr-x  3 root     root       22 Jan 29 02:19 ..
-rw-------  1 ec2-user ec2-user 2067 Feb 17 22:24 .viminfo
drwxrwxr-x  3 ec2-user ec2-user   19 Mar 25 20:44 .local
drwxrwxr-x  3 ec2-user ec2-user   22 Mar 25 20:44 .config
drwxrwxr-x  3 ec2-user ec2-user   22 Mar 25 20:44 .cache
drwx------  2 ec2-user ec2-user   80 Mar 25 21:49 .ssh
-rw-------  1 ec2-user ec2-user 9997 Apr 11 18:50 .bash_history
-rw-rw-r--  1 ec2-user ec2-user  611 Apr 12 14:18 .pearrc
drwx------  8 ec2-user ec2-user  193 Apr 12 14:18 .
drwxrwxr-x 10 ec2-user apache    122 Apr 12 14:19 pear  // as you can see my pear folder has the correct permissions.

[ec2-user@ip-172-31-90-218 ~]$ cd pear
[ec2-user@ip-172-31-90-218 pear]$ ls
bin  cfg  data  docs  man  php.ini-gopear  share  tests  www

[ec2-user@ip-172-31-90-218 pear]$ cd share
[ec2-user@ip-172-31-90-218 share]$ ls
pear

[ec2-user@ip-172-31-90-218 share]$ cd pear
[ec2-user@ip-172-31-90-218 pear]$ ls -alrt
total 108
drwxrwxr-x  3 ec2-user apache    18 Apr 12 14:18 ..
drwxrwxr-x  3 ec2-user apache   108 Apr 12 14:18 .channels
drwxrwxr-x  2 ec2-user apache    21 Apr 12 14:18 Archive
drwxrwxr-x  2 ec2-user apache    24 Apr 12 14:18 Console
drwxrwxr-x  3 ec2-user apache    36 Apr 12 14:18 Structures
drwxrwxr-x  2 ec2-user apache    22 Apr 12 14:18 XML
drwxrwxr-x  2 ec2-user apache    23 Apr 12 14:18 OS
-rw-rw-r--  1 ec2-user apache 20562 Apr 12 14:18 System.php
-rw-rw-r--  1 ec2-user apache  1049 Apr 12 14:18 peclcmd.php
-rw-rw-r--  1 ec2-user apache 35467 Apr 12 14:18 PEAR.php
-rw-rw-r--  1 ec2-user apache 15052 Apr 12 14:18 pearcmd.php
drwxrwxr-x 11 ec2-user apache  4096 Apr 12 14:18 PEAR
drwxrwxr-x  5 ec2-user apache   214 Apr 12 14:21 .registry
drwxrwxr-x  2 ec2-user apache   126 Apr 12 14:21 Mail
-rw-rw-r--  1 ec2-user apache     0 Apr 12 14:21 .lock
-rw-rw-r--  1 ec2-user apache  7542 Apr 12 14:21 .filemap
-rw-rw-r--  1 ec2-user apache     0 Apr 12 14:21 .depdblock
-rw-rw-r--  1 ec2-user apache  2727 Apr 12 14:21 .depdb
-rw-rw-r--  1 ec2-user apache  9878 Apr 12 14:40 Mail.php    // Mail.php should be accessible by my script. 
drwxrwxr-x 11 ec2-user apache   282 Apr 12 14:40 .

脚本通过 JavaScript 运行,通过单击一个按钮。

谁能告诉我为什么在执行 chown 命令后会出现权限错误?

【问题讨论】:

  • 尝试将权限设置为 755:chmod -R 755 /path/here
  • 嗨蒂姆,仍然遇到同样的错误。
  • 网络服务器用户对您的主目录没有任何权限,为什么要这样做?将文件放在 webroot 或网络服务器具有读取权限的其他位置。
  • 你为什么不把 PEAR 相关的东西安装在它所属的地方:在 web 目录中?如果您拒绝移动库,我假设您必须为完整文件夹树提供适当的权限
  • 嗨@BrentHeigold,我遇到了类似的问题,我解决的方法是在 nautilus 中打开 htdocs 文件夹,并在 root 用户中将文件权限设置为读写。

标签: php email pear


【解决方案1】:

我会将其移至答案,因为这似乎是通过仔细检查日志来解决您的问题的方法。

chmod -R 755 pear

chmod 755 设置文件的 755 权限。 755 意味着拥有者的完全权限和其他人的读取和执行权限。

换句话说,仅仅因为您是所有者并不意味着您拥有权限。

【讨论】:

  • 嗨蒂姆,我得到 chmod:无法访问“ec2-user:apache”:没有这样的文件或目录。但是即使在执行“sudo chmod -R 755 pear”之后它仍然不起作用。
  • 然后尝试 chmoding 到 pear 文件夹的完整路径。还要确保你 chown 和 chmod 服务器上的项目文件夹。
  • 嗨,蒂姆,我尝试了“sudo chmod -755 /home/ec2-user/pear/share/pear/Mail.php”,但仍然出现错误。
猜你喜欢
  • 2015-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-27
相关资源
最近更新 更多