如何检查在终端(PHPUnit)中运行的php代码,如果在Apache中运行的php代码能够写入文件夹?

I have file_put_contents code in PHPUnit that can always successfully write a file to a folder with permissions 744.

I have identical file_put_contents website code that cannot write to the same folder unless I max out permissions to 777.

The PHPUnit code is giving me a sort of false positive.

You are running the unit test and the website under two different user accounts. The one you use for your unit tests has permission to write to your folder, the other one doesn't.

There are two possibilities I see here:

  • Run the unit test with the webserver-user
  • Change the folder permissions

Running as a different user

Using sudo (or su), you can change the user which will run the command. In doing so, both operations have the same permissions. If one fails, the other will too.

sudo -u username command

Change the folder permissions

Unix has a user and a group assigned to each file and folder. First, create a group for both your users:

groupadd WebsiteGroup

The add your users to that group

usermod -aG WebsiteGroup user1
usermod -aG WebsiteGroup user2

Now assign the group to your folder using chown/chgrp. Now when you run your tasks, the group permission is the one that determines success or failure for both.

Man Pages for