linux-php shell_exec () system () execution unzip unsuccessful

Source: Internet
Author: User
Business has a need to do file operations, through PHP and Shell Interactive implementation
System (), Shell_exec () performs other systems such as Mv,cp,list, and PHP sets the appropriate security configuration to allow PHP system functions to operate, but it is not always successful when extracting a zip file.
Permissions are also changed to all permissions through A+w.
The commands I've tried are as follows

shell_exec("unzip " . BASE_PATH . "test/test.zip");//system('echo "密码" | 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');

The script is then accessed through the browser.
I ask you, is there any place you have not noticed?

++++++++++++++++++++++++++++++++++++++++++++++++++++++++
The problem is resolved by changing the current working directory before executing shell_exec ().

chdir(BASE_PATH. "test/");

However, there are other problems, why the current PHP directory is base_path[my project root directory]?

Reply content:

Business has a need to do file operations, through PHP and Shell Interactive implementation
System (), Shell_exec () performs other systems such as Mv,cp,list, and PHP sets the appropriate security configuration to allow PHP system functions to operate, but it is not always successful when extracting a zip file.
Permissions are also changed to all permissions through A+w.
The commands I've tried are as follows

shell_exec("unzip " . BASE_PATH . "test/test.zip");//system('echo "密码" | 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');

The script is then accessed through the browser.
I ask you, is there any place you have not noticed?

++++++++++++++++++++++++++++++++++++++++++++++++++++++++
The problem is resolved by changing the current working directory before executing shell_exec ().

chdir(BASE_PATH. "test/");

However, there are other problems, why the current PHP directory is base_path[my project root directory]?

This is PHP the constant, ah, the system should not have BASE_PATH this constant, it should be the framework you use or some of the files you contain define this constant. You can use get_defined_constants() methods to get the constants that are already defined.

Why should we use chdir it first?

    1. Because unzip this command by default is to extract the files into the current directory, rather than the directory of the zip file, so you need to know in which directory now, you can use system('pwd'); to view, and then see your Web users have no corresponding permissions to this directory, in theory, this directory should be you this PHPthe directory where the program is located, but if you are in use Cli mode, that is your current directory, and where the PHP program is irrelevant.

    2. Of course, can not do chdir , it requires you to unzip have a corresponding understanding of the order, in fact, add a-d

      You can put the extracted files into the specified directory.

  • Related Article

    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.