无法在 CentOS 7 上的 PHP 中打开 file_put_contents 上的流

2024-04-13php开发问题
21

本文介绍了无法在 CentOS 7 上的 PHP 中打开 file_put_contents 上的流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我知道这是一个有很多线程的常见错误,但我厌倦了搜索,但我仍然没有解决它.当我尝试运行函数 file_put_contents 时,我收到以下错误:

I know this is a common bug with many threads but I'm tired of searching and I still haven't solved it. When I try to run the function file_put_contents I get the following error:

Warning: file_put_contents(test.txt): failed to open stream: Permission denied in /var/www/html/nurses/adminWriter.php on line 29

我已经做了最常见的回答,把nurses文件夹和test.txt文件的权限改成777.那个不行.

I've already done the most common answer, change the permissions of the nurses folder and the test.txt file to 777. That didn't work.

我目前正在全新安装最新的 CentOS 7 服务器.

I'm currently on a fresh install of the latest CentOS 7 server.

我发现如果我通过控制台运行php脚本:

I've found that if I run the php script through the console:

# php /var/www/html/nurses/adminWriter.php

它工作得很好,我只有在通过浏览器运行它时才会得到错误,但我真的需要这样运行它.

It works just fine, I get the error only when running it through the browser, but I really need to run it this way.

最好的问候!

推荐答案

好的,我找到了问题:selinux

Ok, I found the issue: selinux

解决方案如下:http://kb.sp.parallels.com/en/11142

万一页面宕机:

症状

在您更改存储网站内容的默认目录(例如,更改为/home")后,您将无法通过 Web 访问子域和其他网站的内容.

After you change the default directory to store website content (say, to "/home"), you are unable to access the content of subdomains and additional websites over the Web.

原因

如果SELinuxEnforcement"模式下工作,则会出现此问题.在这种模式下,网站的 webroot 目录和其中的子域具有不同的权限,您的 Web 服务器无法访问它们.运行这个命令:

This problem occurs if SELinux works in "Enforcement" mode. In this mode, webroot directories for websites and subdomains inside them have different permissions, and your web server fails to access them. Run this command:

# getenforce

输出应该是:Enforcing

分辨率

禁用SELinux支持:在任意编辑器中打开文件/etc/selinux/config并找到这一行:

Disable SELinux support: Open the file /etc/selinux/config in any editor and find this row:

SELINUX=<value>

改成:

SELINUX=disabled

如果找不到此文件,请在任意编辑器中打开文件/boot/grub/grub.conf 并将参数 selinux=0 添加到 Grub Boot Loader:

If you cannot find this file, open the file /boot/grub/grub.conf in any editor and add the parameter selinux=0 to the Grub Boot Loader:

title SE-Linux Test System
root (hd0,0)
kernel /boot/vmlinuz-2.4.20-selinux-XXXXXXXXXX ro root=/dev/hda1 nousb selinux=0
#initrd /boot/initrd-2.4.20-selinux-XXXXXXXXXX.img

重新启动服务器以应用更改.如果您不想重新启动,请运行以下命令:

Reboot the server to apply the changes. If you do not want to reboot, run this command:

# setenforce 0

这篇关于无法在 CentOS 7 上的 PHP 中打开 file_put_contents 上的流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17