Adam11
Adam11

Reputation: 491

Why can't PHP create a file, even with 777 permissions?

I have a virtual Arch Linux test server with XAMPP running on my laptop, and I can't get php to create new files, even with permissions set at 777. Here's the code:

$CompleteFilepath = $AFilepath . '/filepath/filename.php';
$FileHandle = fopen($CompleteFilepath, 'c')
    or
die ("cannot be opened for writing"); // Debug

There's obviously more code after, but it's irrelevant because it always dies here. I couldn't get it to work by modifying the owner and group settings, so I finally resorted to recursively setting everything (except for the main root folder) to 777, and it still won't work. But the folder isn't being created directly in the root, so it shouldn't matter, right?

Edit: I'm still not really sure what I was doing wrong, but today I set the owner to the server and it worked. I thought I had tried that, but maybe not. At least I can get on with development, even if I need to configure my production server more securely in the end.

Upvotes: 1

Views: 4879

Answers (4)

nick
nick

Reputation: 3700

I had a similar problem with a clean install of CentOS 7. It turned out it had SELinux installed by default, which was preventing all PHP writes, even when directory permissions were correct.

Test if SELinux is installed: sestatus

Put SELinux in permissive mode: setenforce 0

Upvotes: 3

StreakyCobra
StreakyCobra

Reputation: 2001

PHP has a directive named open_basedir which permit to limit the access on filesystem to specified directory-tree.

For example if you have in your php.ini:

open_basedir = /srv/http/:/home/:/tmp/

and you will access the file /filepath/filename.php, then you must set:

open_basedir = /srv/http/:/home/:/tmp/:/filepath/

otherwise PHP will not be able to access the file.

In this way that will allow all PHP code running on this machine to access /filepath/ directory-tree, so it is not secure for production environment. The better way is to set virtual-host based open_basedir in httpd.conf. I let you check your HTTP server documentation and also open_basedir documentation for that.

Upvotes: 0

Adam11
Adam11

Reputation: 491

Well, I set the owner to the server and now it works. I thought I had tried that already, but apparently not. Still not sure exactly what I was doing wrong but at least now it works so I can get on with development.

Upvotes: 0

itsid
itsid

Reputation: 811

what php version is running, afaik option 'c' is only available with php 5.2.6 and up.

or short: does

$FileHandle = fopen($CompleteFilepath, 'w');

work?

Upvotes: 0

Related Questions