Is there a way to make files in a compressed archive writeable after extraction?
Asked by
Vincentt (
8094)
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
Unless I’m misunderstanding, you could use chmod() in PHP to modify the file permissions after extracting them.
http://uk.php.net/chmod
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.
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?
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.
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.
I wonder who “owns” the files :p
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 :)
@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.
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?
Ah, that’s an idea: I set the permissions to 777 before extraction and then make them 755 when the script is run. Thanks! :)
It depends really, I see you’ve found a good way to do it though and I like that way :)
Yeah, now I just need to find out how to get Phing to preserve file permissions when tarring ;-)
Can you just run “regular” tar by tripping a shell command with shell_exec()?
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
This question is in the General Section. Responses must be helpful and on-topic.