tags:

views:

38

answers:

1

I am trying to ultimately extract some files from a tar archive within PHP, however my simple test script is failing at the first part, which was to simply list the files in the archive.

Is there any reason why this test script fails?

What I see output is:

> -sh-3.2$ php showfile.php /var/www/vhosts/smartphonesoft.com/httpdocs/fred/epf/itunes20100825.tbz
> tar: Old option `f' requires an
> argument. Try `tar --help' or `tar
> --usage' for more information.

My simple PHP script is:

<?php 
foreach (glob("/var/www/vhosts/smartphonesoft.com/httpdocs/fred/epf/itunes*.tbz") as $file) {
 }
$path_parts = pathinfo($file);
$tarfile = $path_parts['filename'];
echo $file . "\n";
exec('tar tvf $file',$ret);

?>
+4  A: 

you have badly placen the parenthesis, ({ }), and bad quotes inside the exec() function:

<?php 
foreach (glob("/var/www/vhosts/smartphonesoft.com/httpdocs/fred/epf/itunes*.tbz") as $file) {

$path_parts = pathinfo($file);
$tarfile = $path_parts['filename'];
echo $file . "\n";
exec("tar tvf $file",$ret);

}

?>

(using ' quotes does NOT substitute the variable $file inside the string for the file you meant.)

EDIT: according to Frxstrem's comment, never forget to escape the filename (in case it contains for example spaces, the above code won't work).

exec("tar tvf ".escapeshellarg($file), $ret);
Yossarian
escaping is important to remember - you should therefore use `"tar tvf ".escapeshellarg($file)` instead
Frxstrem
@Frxstrem, thanks, I updated my answer.
Yossarian
thanks, there will only be one file there that matches that particular string, hence the parenthesis placement.
kitenski
@kitenski, in that case, do not use empty statement, just put break; between the {} brackes.
Yossarian