Linux-PHPshell_exec () system () failed to execute unzip

Source: Internet
Author: User
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 isPHPThe system should have noBASE_PATHThis 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 usechdirWhat about

  1. BecauseunzipBy 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.PHPThe directory where the program is located. However, if you useCliInPHPIt has nothing to do with where the program is located.

  2. Of course.chdirYou needunzipThe command has a corresponding understanding, in fact add-d

    You can put the decompressed file to the specified directory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.