There is a business requirement for file operations. Through PHP and shell interaction, system () and shell_exec () can execute other system commands, such as mv, cp, and list, PHP sets the corresponding security configuration to allow PHP system function operations, but it is always unsuccessful when extracting the zip file. Permission is also... there is a business requirement, file operations, through PHP and shell interaction.
System (), shell_exec () can execute other system commands such as mv, cp, list, etc. PHP sets the corresponding security configuration to allow PHP system function operations, however, it is always impossible to decompress the zip file.
The permission is changed to all permissions through a + w.
The command I tried is as follows:
Shell_exec ("unzip ". BASE_PATH. "test/test.zip"); // system ('echo "password" | sudo-S unzip '. BASE_PATH. 'test/test.zip '); system ('echo "123456" | sudo-u root-S unzip '. BASE_PATH. 'test/test.zip '); system ('echo "123456" | sudo-S unzip '. BASE_PATH. 'test/test.zip ');
Then access the script in the browser.
I would like to ask you if you have noticed anything else?
++ ++
The problem is solved. You need to change the current working directory before executing shell_exec.
chdir(BASE_PATH. "test/");
However, why is the current PHP Directory BASE_PATH [my project root directory]?
Reply content:
There is a business requirement to perform file operations through PHP and shell interaction.
System (), shell_exec () can execute other system commands such as mv, cp, list, etc. PHP sets the corresponding security configuration to allow PHP system function operations, however, it is always impossible to decompress the zip file.
The permission is changed to all permissions through a + w.
The command I tried is as follows:
Shell_exec ("unzip ". BASE_PATH. "test/test.zip"); // system ('echo "password" | sudo-S unzip '. BASE_PATH. 'test/test.zip '); system ('echo "123456" | sudo-u root-S unzip '. BASE_PATH. 'test/test.zip '); system ('echo "123456" | sudo-S unzip '. BASE_PATH. 'test/test.zip ');
Then access the script in the browser.
I would like to ask you if you have noticed anything else?
++ ++
The problem is solved. You need to change the current working directory before executing shell_exec.
chdir(BASE_PATH. "test/");
However, why is the current PHP Directory BASE_PATH [my project root directory]?
This isPHP
The system should have noBASE_PATH
This constant should be defined in the framework you are using or some files you include. You can useget_defined_constants()
To obtain the defined constants.
Why usechdir
What about
Becauseunzip
By default, this command decompress the file to the current directory, instead of the directory where the zip file is located. Therefore, you need to know which directory the file is in. You can usesystem('pwd');
Check the Directory and check whether your Web user has the corresponding permissions for the directory. In theory, this directory should be yours.PHP
The directory where the program is located. However, if you useCli
InPHP
It has nothing to do with where the program is located.
Of course.chdir
You needunzip
The command has a corresponding understanding, in fact add-d
You can put the decompressed file to the specified directory.