php exec delete files from rar
I'm trying to remove a file from a rar archive.
if you run /usr/bin/rar d rar_file.rar del.txt
this command deletes the file del.txt from rar_file.rar
I'm tring to run this in php using exec(), but it's not working.
<?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);
?>
Here's my output:
/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
)
Didn't delete the teste.txt file from the rar.
But, I copied the command /usr/bin/rar d '/var/www/temp/temp.rar' teste.txt
and executed in terminal.
Here's my output:
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
Works perfectly, but in php exec didn't. I don't think it's file permissions cause the user www-data created the rar file (I created before) and I set the permissions to 777 without problems.
I'm using linux, sure.
What I'm doing wrong? thanks!
The solution:
When you execute something using exec(), for some reason the exec() stops before the script finish... This appends with many scripts that I've did.
So, the solution, by mario, helps me, adding a 2>&1
in the command, exec() only stop when the script finishes.
精彩评论