php file_put_contents函数不起作用

时间:2022-09-29 13:23:35

Why would file_put_contents refuse to work for the following code?

为什么file_put_contents拒绝为以下代码工作?

$f = file_put_contents("files/r.js", "Some text here");

if ($f) print 1;
else print 0;

4 个解决方案

#1


39  

It could be a permission problem

Is the directory /files chmoded to 777? sometimes php won't allow you to access directories if they don't have enough permission. I don't know about blank errors though.

目录/文件是否已订到777?有时php不允许您访问目录,如果它们没有足够的权限。我不知道什么是空白错误。

Try to see if it has enough permissions, if not, then set it to 777 and try it.

试着看看它是否有足够的权限,如果没有,那么将它设置为777并尝试它。

#2


4  

Are you using the full path on the filesystem or are you trying to use the URI? I think this PHP function expects you to give the path as the file is found on the filesystem.

您是在文件系统上使用完整路径,还是在尝试使用URI?我认为这个PHP函数期望您在文件系统上找到文件时给出路径。

Relative paths should be okay though.

相对路径应该没问题。

You may need to make sure the file exists and that it's permissions are set to 777. Sometimes I've found that it's not enough to just have the directory permissions set to 777 but the file must also already exist.

您可能需要确保该文件存在,并将其权限设置为777。有时,我发现仅仅将目录权限设置为777是不够的,但是文件也必须已经存在。

#3


1  

We've experienced this, requiring a workaround (regardless of method, permissions and anything else mentioned here). When all other fixes failed, we have found it can be linked to restrictions created by SELinux.

我们已经经历了这一点,需要一个变通方法(不考虑方法、权限和这里提到的其他内容)。当所有其他修复失败时,我们发现它可以链接到SELinux创建的限制。

#4


0  

If you’re using windows the following solution worked perfectly for me on Windows 10 running php 5.5.38

如果您正在使用windows,下面的解决方案在运行php 5.5.38的windows 10上运行得非常好

If you’re having this problem on Windows/IIS try the following:

如果您在Windows/IIS上遇到此问题,请尝试以下操作:

1 Go to the Folder that you are trying to write to and right click it, then select properties. 2 Select the Security Tab 3 Click Edit 4 Click Add 5 Click Advanced 6 Click Find Now 7 From the list of User select IUSR and click OK 8 Click OK again. 9 The IUSR will be displayed in the top box labelled 'Group of User Names' 10 Select IUSR and grant necessary permissions in the 'Permissions for BATCH' list view. 11 Click Apply and then you are done.

到要写入的文件夹,右键单击,然后选择properties。2选择安全选项卡3点击编辑4点击添加5点击高级6点击现在7从用户列表中选择IUSR再点击确定8再次点击确定。9 IUSR将显示在“用户名组”的顶部框中,选择IUSR并在“批处理”列表视图中授予必要的权限。单击Apply,就完成了。

The steps may be slightly different for different versions of windows. This also applies to ASP.NET though I think the users you add are the network users (NETWORK AND OR NETWORK SERVICE users) as well as the IUSR.

不同版本的windows操作系统的步骤可能略有不同。这也适用于ASP。尽管我认为您添加的用户是网络用户(网络和网络服务用户)和IUSR。

#1


39  

It could be a permission problem

Is the directory /files chmoded to 777? sometimes php won't allow you to access directories if they don't have enough permission. I don't know about blank errors though.

目录/文件是否已订到777?有时php不允许您访问目录,如果它们没有足够的权限。我不知道什么是空白错误。

Try to see if it has enough permissions, if not, then set it to 777 and try it.

试着看看它是否有足够的权限,如果没有,那么将它设置为777并尝试它。

#2


4  

Are you using the full path on the filesystem or are you trying to use the URI? I think this PHP function expects you to give the path as the file is found on the filesystem.

您是在文件系统上使用完整路径,还是在尝试使用URI?我认为这个PHP函数期望您在文件系统上找到文件时给出路径。

Relative paths should be okay though.

相对路径应该没问题。

You may need to make sure the file exists and that it's permissions are set to 777. Sometimes I've found that it's not enough to just have the directory permissions set to 777 but the file must also already exist.

您可能需要确保该文件存在,并将其权限设置为777。有时,我发现仅仅将目录权限设置为777是不够的,但是文件也必须已经存在。

#3


1  

We've experienced this, requiring a workaround (regardless of method, permissions and anything else mentioned here). When all other fixes failed, we have found it can be linked to restrictions created by SELinux.

我们已经经历了这一点,需要一个变通方法(不考虑方法、权限和这里提到的其他内容)。当所有其他修复失败时,我们发现它可以链接到SELinux创建的限制。

#4


0  

If you’re using windows the following solution worked perfectly for me on Windows 10 running php 5.5.38

如果您正在使用windows,下面的解决方案在运行php 5.5.38的windows 10上运行得非常好

If you’re having this problem on Windows/IIS try the following:

如果您在Windows/IIS上遇到此问题,请尝试以下操作:

1 Go to the Folder that you are trying to write to and right click it, then select properties. 2 Select the Security Tab 3 Click Edit 4 Click Add 5 Click Advanced 6 Click Find Now 7 From the list of User select IUSR and click OK 8 Click OK again. 9 The IUSR will be displayed in the top box labelled 'Group of User Names' 10 Select IUSR and grant necessary permissions in the 'Permissions for BATCH' list view. 11 Click Apply and then you are done.

到要写入的文件夹,右键单击,然后选择properties。2选择安全选项卡3点击编辑4点击添加5点击高级6点击现在7从用户列表中选择IUSR再点击确定8再次点击确定。9 IUSR将显示在“用户名组”的顶部框中,选择IUSR并在“批处理”列表视图中授予必要的权限。单击Apply,就完成了。

The steps may be slightly different for different versions of windows. This also applies to ASP.NET though I think the users you add are the network users (NETWORK AND OR NETWORK SERVICE users) as well as the IUSR.

不同版本的windows操作系统的步骤可能略有不同。这也适用于ASP。尽管我认为您添加的用户是网络用户(网络和网络服务用户)和IUSR。