Solution to php exec error: First change the user of the file to php user; then modify the content of "exec()" to "exec('7za x /home/www/test/filename.csv.7z -r -o./report/sogou/');" that's it.
Recommended: "PHP Video Tutorial"
Problems encountered when executing exec in PHP Pit (linux permission pit
今天在使用php中的exec()命令,下载搜狗推广数据报表压缩包,解压的时候遇到一个坑,搜狗给了一个链接,在浏览器访问的时候直接下载的是.zip的压缩包,于是使用
//下载压缩包 exec("wget -O /home/www/test/filename.zip 'sougou_url'");//注意url要用单引号包含住 //解压 exec('unzip -o /home/www/test/filename.zip);
The result is an error: decompression failed, so I went to the shell terminal to use the command to decompress and still got an error as shown:
After asking the operation and maintenance, they said that the file format is not in zip format. After trying various decompression commands, it turned out to be a compressed package in .7z format. This has to be said that Sogou is a bit messed up.
Because the decompression tool under windows is integrated with the 7z format, no error is reported when decompressing under windows:
Now that the problem has been found, it can be solved ( Note: There is no p7zip on centos by default and needs to be installed (yum install -y p7zip);
Then modify the above exec() as follows:
exec("wget -O /home/www/test/filename.7z 'sogou_url'"); chmod("./report/sogou/tmp/filename.7z",0777);//此处要给权限否则下一个命令无法执行 exec('7za x /home/www/test/filename.csv.7z -r -o./report/sogou/');
Remember the user permissions of the file, because I The root user is used in the terminal. After modifying the file permissions, the program will report an error when running exec to operate the file without permissions. This is because the user to which php belongs is not root. You only need to change the user of the file to the php user.
The above is the detailed content of How to solve php exec error problem. For more information, please follow other related articles on the PHP Chinese website!