所以我试着用Docker构建一个LAMP应用程序,因为在我的本地机器上安装PHP之类的东西总是有问题。
目前,我正在学习Brad的教程: youtube上的PHP速成课程,我被困在了他解释“文件处理”的部分。第14部分
这是我正在犯的错误:
Warning: fopen(users.txt): Failed to open stream: Permission denied in /var/www/html/filehandling.php on line 13
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文件。
当我用'whoami‘查找用户时,我会在Docker容器中获得用户’‘root’。当我在容器外尝试'whoami‘时,我得到了我的常规用户名。
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时得到的结果:
# 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用户时得到的:
# 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中更改这些内容,当然不是同时进行:
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
但没有成功-
发布于 2022-11-25 08:28:59
我已将Dockerfile更改为:
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”。这起作用了!
发布于 2022-11-20 12:57:05
fopen()
函数打开一个文件或URL。
语法
fopen(filename, mode, include_path, context)
示例
<?php
$file = fopen("test.txt", "r");
//Output lines until EOF is reached
while(! feof($file)) {
$line = fgets($file);
echo $line. "<br>";
}
fclose($file);
?>
您的问题:最有可能是在另一个目录中查找代码,因此必须显式地指定目录以避免此类错误。它还必须确保获得必要的权限。
fopen('/path/to/file/test.txt','r');
您可以通过使用一些使获取目录更容易的函数来简化此操作。
fopen($_SERVER['DOCUMENT_ROOT'].'test.txt','r');
参考文献:
https://stackoverflow.com/questions/74510845
复制相似问题