fopen()无法在Linux上创建文件 [英] fopen() fails to create a file on linux

查看:99
本文介绍了fopen()无法在Linux上创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 fopen()创建文件,如下所示:

I'm trying to create a file through fopen() as below :

<?php 
  $handle = fopen('name.txt', 'w') or die('Can\'t create file');
  fwrite($handle, 'Hamed'."\n");
  fwrite($handle, 'Kamrava'."\n");
  echo 'File was created successfully';
?>

但是不会创建该文件并得到我:

But doesn't create that file and get me:

无法创建文件

PS:

我正在Linux/Ubuntu上使用LAMP服务器.

I'm using LAMP server on Linux/Ubuntu.

在创建该文件之前,我已经尝试过以下命令:

I've tried below command before creating that file:

sudo chmod -R 755 ~/public_html/

任何想法都会受到赞赏.

Any ideas would be appreciated.

推荐答案

PHP脚本以为Apache Web服务器配置的用户身份执行.为了创建文件,该用户必须对要在其中创建文件的目录具有写权限.有两种方法可以做到这一点:

The PHP script is executed as the user configured for the Apache web server. In order to create files, this user has to have write permission to the directory in which you want to create the file. There are two ways to do this:

您可以为所有用户授予对该目录的写权限,其中包括为Apache配置的任何内容(对于开发机来说已经足够了):

You can give the write permission to the directory for all users, which will include whatever is configured for Apache (good enough for a development machine):

chmod o+w ~/public_html/directory_where_you_want_to_write

或者,您可以将目录的所有权传递给Apache用户.这意味着使用常规用户帐户的 you 不能在此目录中创建或删除文件,除非您也执行上述操作.假设Apache使用 www-data 用户帐户运行,就像默认情况下在Ubuntu上一样,您将执行以下操作:

Or you can pass the ownership of the directory to the Apache user. This will mean that you with your regular user account won't be able create or delete files in this directory though, unless you also do the above. Assuming Apache runs with the www-data user account, like it does by default on Ubuntu, you would do:

sudo chown www-data ~/public_html/directory_where_you_want_to_write

(而且目录不必位于 public_html 下,我只是以它为例,因为这似乎是您的文件所在的位置.)

(And the directory doesn't have to be under public_html, I'm just using it as an example since that seems to be where your files are.)

这篇关于fopen()无法在Linux上创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆