General Question

Vincentt's avatar

Is there a way to make files in a compressed archive writeable after extraction?

Asked by Vincentt (8094points) August 4th, 2008

I’m doing a PHP project which uses configuration files. Now, these files need to be writeable by my PHP scripts. So what I’d like to know is: is it possible to set permissions on certain files before compressing them so that they can be written to by a PHP script after they have been extracted and uploaded to a webserver?

Observing members: 0 Composing members: 0

14 Answers

richardhenry's avatar

Unless I’m misunderstanding, you could use chmod() in PHP to modify the file permissions after extracting them.

http://uk.php.net/chmod

Breefield's avatar

Although I haven’t done this either, I’d use chmod($filename, 0755) which is read and write for owner, read and execute for others.

Vincentt's avatar

But wouldn’t you already need write permissions before you can change the permissions?

And I can set the permissions to 755 before archiving them, but how do I know the owner is the same user as the one the PHP script is executing as?

Breefield's avatar

I’m not sure about all that permission stuff actually, maybe google it a bit. But I do know you can (through PHP) change a file’s permissions no matter what your “status” is.

Vincentt's avatar

That’d be very handy but concerning at the same time (if PHP could circumvent the filesystem permissions). The PHP website says:

> The current user is the user under which PHP runs.
> It is probably not the same user you use for normal shell or FTP access.
> The mode can be changed only by user who owns the file on most systems.

Breefield's avatar

I wonder who “owns” the files :p

Skyrail's avatar

It’ll be whatever the web server is running under which varies from OS to OS (including between distros) some don’t even run it under a user just to be safe :)

Vincentt's avatar

@Skyrail – hmm, on my local Apache installation it’s www-data. Is this the default for all Apache installations? If so, that’d be a reasonable target, with instructions for people that do not use Apache on Linux on updating the permissions.

chaosrob's avatar

I would using chmod() would be the right approach. Relying on the files to be in a writable state after another process has been at them seems likely to cause problems. Why not just set the permissions you need on the fly?

Vincentt's avatar

Ah, that’s an idea: I set the permissions to 777 before extraction and then make them 755 when the script is run. Thanks! :)

Skyrail's avatar

It depends really, I see you’ve found a good way to do it though and I like that way :)

Vincentt's avatar

Yeah, now I just need to find out how to get Phing to preserve file permissions when tarring ;-)

chaosrob's avatar

Can you just run “regular” tar by tripping a shell command with shell_exec()?

Vincentt's avatar

I could get it to execute shell commands, though I’d prefer using built-in support. Unfortunately, it depends on a PEAR module for tarring that doesn’t support permissions yet (as noted in the Phing source code).

However, I just extracted the generated archive (so without permissions explicitly set) and uploaded them with FTP, and my script could perfectly well edit the files, so I guess I’m satisfied :)

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther