You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 16.04下PHP使用fopen和fwrite无法读写文件求助

Troubleshooting File Write Issues on Ubuntu 16.04

Hey there, let's break down why you're having trouble writing to data.txt—first the permission denied error, then no error but no write after opening up permissions completely. Here are the most likely causes and fixes:

1. Why the initial -rw-r--r-- permission failed

The -rw-r--r-- (octal 644) permission means:

  • The file's owner has read/write access
  • The group owner and all other users only have read access

If the user running your code isn't the owner of data.txt, it makes total sense you got a Permission denied error—those users can't write to the file.

2. Why -rw-rw-rw- (octal 666) isn't working (no error, no write)

Opening up permissions to full read/write for everyone should remove basic permission blocks, so the issue is likely something else:

- AppArmor (Ubuntu's security module) is blocking writes

Ubuntu 16.04 uses AppArmor by default, which can restrict what applications (like PHP, since your username suggests you're using it) can do. It might be preventing your code from writing to that specific file even if filesystem permissions allow it.

To test this temporarily, stop AppArmor:

sudo systemctl stop apparmor

Then try running your code again. If it writes successfully now, you'll need to update AppArmor's rules to allow access to data.txt instead of leaving AppArmor disabled long-term.

- Parent directory permissions are too restrictive

Even if the file has 666 permissions, your code's user needs execute (x) permission on the parent directory to access files inside it. If the directory lacks this, your code might not even reach the file properly (but fail silently depending on your code's error handling).

Check the directory permissions with:

ls -ld /path/to/the/folder/containing/data.txt

Make sure the user running your code has at least x access to the directory.

If data.txt is a soft link, you might have modified the link's permissions but not the target file it points to. Run this to check:

ls -l data.txt

If it starts with l, it's a link. Follow the path to the target file and check its permissions and existence.

- Your code is using the wrong file path

If you're using a relative path (like just data.txt) in your code, the working directory when your code runs might not be the one you expect. For example, if you're running PHP via Apache, the working directory could be /var/www/html instead of your local project folder.

In your code, add a line to print the full path of the file you're trying to write to. For PHP:

echo realpath('data.txt');

Check if this matches the data.txt you modified permissions on.

- Disk is full or filesystem is read-only

A full disk will prevent writes, and a read-only mounted filesystem won't allow any changes at all. Check disk space:

df -h

And check if the filesystem is mounted read-only:

mount | grep /path/to/your/filesystem

Look for the ro flag in the output—if it's there, you'll need to remount it as read-write.

3. Better long-term fix (avoid 666 permissions!)

Setting files to 666 is a security risk—anyone on the system can modify them. Instead:

  • Change the file's owner to the user running your code:
    sudo chown your-code-user:your-code-user data.txt
    
  • Or add the code's user to the file's group:
    sudo usermod -aG file-group your-code-user
    

Then set reasonable permissions like 664 (owner/group read-write, others read) or 640 (owner read-write, group read).

内容的提问来源于stack exchange,提问作者php12345

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:52:48