Hello,
I am using proc_open() to execute a 7zip command that creates a ZIP archive (or adds to it if the destination ZIP file already exists).
If the ZIP file does not already exist, it is created without issue. However, if the file does exist, when 7zip attempts to add the new file to the existing archive, the command fails with exit code 2 (fatal error).
Here is the input:
7z a -r -mmt -mx0 -y -tzip "/var/www/example.com/tmp/19I1m4.zip" "/var/www/example.com/tmp/qgy546/Directory to be Zipped/"
And the output:
7-Zip 9.04 beta Copyright (c) 1999-2009 Igor Pavlov 2009-05-30
p7zip Version 9.04 (locale=C,Utf16=off,HugeFiles=on,16 CPUs)
Scanning
Updating archive /var/www/example.com/tmp/19I1m4.zip
Error:
Can not open file
19I1m4.zip.tmp
Permission denied
System error:
E_FAIL
This seems to be a classic permissions problem. But I'm a bit confused as to why PHP is able to create the ZIP file but not modify it.
It's clear from the 7zip output that 7zip is attempting to create a temporary file with the same name and a ".tmp" extension, and this seems to be where the failure occurs. Given that the parent directory has 0777 permissions, shouldn't the www-data user be permitted to create files there? The same user just created the original ZIP file in the same location without issue.
Just to debug the issue, I set the permissions on the directory to which the ZIP file is written to 0777. Here is the ls -lah output:
drwxrwxrwx 5 web5 client1 4.0K Nov 7 22:07 .
drwxr-x--- 12 web5 client1 4.0K Nov 7 21:43 ..
-rwxrwxrwx 1 www-data www-data 7.3M Nov 7 22:07 19I1m4.zip
drwxr-xr-x 3 www-data www-data 4.0K Nov 7 22:07 qgy546
I also added a call to set the permissions on the ZIP file to 777 after it is created: chmod($zip->dest, octdec(777)).
To say a bit more about the users and groups in the output above, PHP runs via Apache's mod_php, and hence, shell commands are executed as the www-data user, who is a member of the client1 group.
I know that the application logic is sound because this works as expected on Windows if Apache is running with Administrator permissions.
Does anything jump-out at anybody? Any help is very much appreciated.