首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >无法在PHP中打开文件,权限被拒绝,还可以使用fwrite()键入错误

无法在PHP中打开文件,权限被拒绝,还可以使用fwrite()键入错误
EN

Stack Overflow用户
提问于 2022-11-20 10:34:36
回答 2查看 60关注 0票数 0

所以我试着用Docker构建一个LAMP应用程序,因为在我的本地机器上安装PHP之类的东西总是有问题。

目前,我正在学习Brad的教程: youtube上的PHP速成课程,我被困在了他解释“文件处理”的部分。第14部分

这是我正在犯的错误:

代码语言:javascript
运行
AI代码解释
复制
Warning: fopen(users.txt): Failed to open stream: Permission denied in /var/www/html/filehandling.php on line 13
代码语言:javascript
运行
AI代码解释
复制
Fatal error: Uncaught TypeError: fwrite(): Argument #1 ($stream) must be of type resource, bool given in /var/www/html/filehandling.php:15 Stack trace: #0 /var/www/html/filehandling.php(15): fwrite(false, 'Brad') #1 {main} thrown in /var/www/html/filehandling.php on line 15

我还添加了错误和文件本身的屏幕截图、我的Dockerfile和docker-compose.yaml文件。

浏览器中的错误消息

filehandling.php

Dockerfile

docker-compose.yml

当我用'whoami‘查找用户时,我会在Docker容器中获得用户’‘root’。当我在容器外尝试'whoami‘时,我得到了我的常规用户名。

代码语言:javascript
运行
AI代码解释
复制
I have tried changing permissions with chmod 755 and chmod 777 inside the Dockerfile, but that didn't work either.

有人能帮忙吗?

提前谢谢你!

下面是@hakre的建议:这是我在容器外壳中执行ls -altrh时得到的信息:

这就是我在容器中做ls -altrh时得到的结果:

代码语言:javascript
运行
AI代码解释
复制
# ls -altrh
total 68K
drwxr-xr-x 1 root root 4.0K Sep 13 09:45 ..
-rwxr-xr-x 1 1000 1000   14 Oct 14 14:57 phpinfo.php
drwxr-xr-x 2 1000 1000 4.0K Oct 14 18:51 db
-rwxr-xr-x 1 1000 1000    0 Oct 15 17:18 .env
-rwxr-xr-x 1 1000 1000   90 Oct 15 17:23 .env.example
-rwxr-xr-x 1 1000 1000    5 Oct 15 17:25 .gitignore
-rwxr-xr-x 1 1000 1000 3.7K Nov  9 17:15 variables.php
-rwxr-xr-x 1 1000 1000  468 Nov  9 17:32 getpost.php
-rwxr-xr-x 1 1000 1000  599 Nov 14 15:15 sanitizinginput.php
-rwxr-xr-x 1 1000 1000  167 Nov 14 15:38 cookies.php
-rwxr-xr-x 1 1000 1000  904 Nov 14 16:03 sessions.php
-rwxr-xr-x 1 1000 1000  225 Nov 19 17:10 package.json
drwxr-xr-x 4 1000 1000 4.0K Nov 20 15:26 .
-rwxr-xr-x 1 1000 1000  734 Nov 20 15:26 docker-compose.yml
-rwxr-xr-x 1 1000 1000  335 Nov 20 15:26 index.php
drwxr-xr-x 2 1000 1000 4.0K Nov 20 16:29 extras
-rwxr-xr-x 1 1000 1000  283 Nov 20 18:07 filehandling.php
-rwxr-xr-x 1 1000 1000   73 Nov 20 18:12 Dockerfile
# 

编辑:这也是我在容器的shell中寻找apache用户时得到的:

代码语言:javascript
运行
AI代码解释
复制
# ps aux | egrep '(apache|httpd)'
root           1  0.3  0.1 219560 28892 ?        Ss   09:28   0:00 apache2 -DFOREGROUND
www-data      23  0.0  0.0 219592  7804 ?        S    09:28   0:00 apache2 -DFOREGROUND
www-data      24  0.0  0.0 219592  7804 ?        S    09:28   0:00 apache2 -DFOREGROUND
www-data      25  0.0  0.0 219592  7804 ?        S    09:28   0:00 apache2 -DFOREGROUND
www-data      26  0.0  0.0 219592  7804 ?        S    09:28   0:00 apache2 -DFOREGROUND
www-data      27  0.0  0.0 219592  7804 ?        S    09:28   0:00 apache2 -DFOREGROUND
root          35  0.0  0.0   3180   652 pts/0    S+   09:29   0:00 grep -E (apache|httpd)

并尝试在Dockerfile中更改这些内容,当然不是同时进行:

代码语言:javascript
运行
AI代码解释
复制
RUN chown -R www-data /var/www/html/ 
and this: RUN chown -R www-data:www-data /var/www/html/ 
and this: RUN chown 777 /var/www/html 
and this: RUN chown 777 www-data /var/www/html 

但没有成功-

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-11-25 08:28:59

我已将Dockerfile更改为:

代码语言:javascript
运行
AI代码解释
复制
FROM php:8.1.10-apache

WORKDIR /var/www/html

RUN docker-php-ext-install mysqli pdo pdo_mysql

RUN chown -R www-data:www-data /var/www

# Create a new user
RUN adduser --disabled-password --gecos '' developer

# Add user to the group
RUN chown -R developer:www-data /var/www

RUN chmod 755 /var/www

# Switch to this user
USER developer

并再次将变量的路径$file更改为“extras/users.txt”。这起作用了!

票数 0
EN

Stack Overflow用户

发布于 2022-11-20 12:57:05

fopen()函数打开一个文件或URL。

语法

代码语言:javascript
运行
AI代码解释
复制
fopen(filename, mode, include_path, context)

示例

代码语言:javascript
运行
AI代码解释
复制
<?php
$file = fopen("test.txt", "r");

//Output lines until EOF is reached
while(! feof($file)) {
  $line = fgets($file);
  echo $line. "<br>";
}

fclose($file);
?>

您的问题:最有可能是在另一个目录中查找代码,因此必须显式地指定目录以避免此类错误。它还必须确保获得必要的权限

代码语言:javascript
运行
AI代码解释
复制
fopen('/path/to/file/test.txt','r');

您可以通过使用一些使获取目录更容易的函数来简化此操作。

代码语言:javascript
运行
AI代码解释
复制
fopen($_SERVER['DOCUMENT_ROOT'].'test.txt','r');

参考文献:

W3Schools函数

PHP: fopen()权限被拒绝

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74510845

复制
相关文章

相似问题

领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文