我正在尝试从rar存档中删除一个文件。如果运行/usr/bin/rar d rar_file.rar del.txt,此命令将从rar_file.rar中删除文件del.txt
我试着用exec()在php中运行它,但是它不工作。
<?php
$file = realpath("temp/temp.rar");
chmod($file, 0777); //make sure I can handle the file
if(file_exists($file)){ //Make sure that the file exists
$rar = escapeshellarg($file);
echo "/usr/bin/rar d $rar teste.txt"; //Lets see the command
exec("/usr/bin/rar d $rar teste.txt", $r);
}
echo "\n";
print_r($r);
?>下面是我的输出:
/usr/bin/rar d '/var/www/temp/temp.rar' teste.txt
Array
(
[0] =>
[1] => RAR 3.90 beta 2 Copyright (c) 1993-2009 Alexander Roshal 3 Jun 2009
[2] => Shareware version Type RAR -? for help
)未从rar中删除teste.txt文件。
但是,我复制了命令/usr/bin/rar d '/var/www/temp/temp.rar' teste.txt并在终端中执行。下面是我的输出:
stive@stive-laptop:~$ /usr/bin/rar d '/var/www//temp/temp.rar' teste.txt
RAR 3.90 beta 2 Copyright (c) 1993-2009 Alexander Roshal 3 Jun 2009
Shareware version Type RAR -? for help
Deleting from /var/www/temp/temp.rar
Deleting teste.txt
Done我不认为是文件权限导致用户www-data创建了rar文件(我之前创建的),我将权限设置为777没有问题。
当然,我使用的是linux。
我哪里做错了?谢谢!
发布于 2011-06-16 07:16:17
解决方案:
当您使用exec()执行某些操作时,由于某种原因,exec()会在脚本结束之前停止...这是我做过的许多脚本的补充。
因此,mario的解决方案帮助了我,在命令中添加了一个2>&1,exec()只有在脚本结束时才会停止。
https://stackoverflow.com/questions/6337766
复制相似问题