2014-12-01 112 views
0

以下命令查找文件的目录,并压缩它,它运作良好PHP shell执行创建动态命令

$command = "cd {$root}/files && mkdir -p {$identifier} && zip -jFS -0 {$root}{$zipname} 2491/'test&.txt'"; 
    exec($command); 

但是修改文件作为变量没有被允许shell执行,下面的代码无法正常工作

$container_name = "2491"; 
$files = Array ('0' => 'test&.txt' ,'1' => 'test5.txt','2' => 'test6.txt'); 
$files = " " . $container_name . "/'" . implode("' " . $container_name . "/'", $files) . "'"; 
$files = str_replace('$', '\$', $files); 
$command = "cd {$root}/files && mkdir -p {$identifier} && zip -jFS -0 {$root}{$zipname} {$files}"; 
exec($command); 

$ root,$ identifier,$ zipname不会导致问题,它的$ files有什么问题?

更新

执行前的var_dump为$命令:

string(128) "cd /var/www/files && mkdir -p zip--1 && zip -jFS -0 /var/www/files/zip--1/1002_22-06022-06022-_content.zip 2491/'test&.txt'" 

其中,如果我执行的

exec("cd /var/www/files && mkdir -p zip--1 && zip -jFS -0 /var/www/files/zip--1/1002_22-06022-06022-_content.zip 2491/'test&.txt'"); 

运行完美

错误的回复:

zip错误:无事可做! (/var/www/files/zip--1/1002_22-06022-06022-_content.zip)

+1

你可以在执行后发布'$ command'变量的输出吗? – preinheimer 2014-12-01 15:51:46

+0

执行该命令之前,转储它并检查它是否与不使用变量时完全相同。 – vaso123 2014-12-01 15:51:46

+0

@preinheimer得到zip错误:无事可做! (/var/www/files/zip--1/1002_22-06022-06022-_content.zip)作为命令执行响应。 – arpitr 2014-12-01 16:04:01

回答

0

我弄明白了,问题是文件名为'&',这个文件名正在追溯到'&',从而违反了命令。为了得到它,我使用[htmlspecialchars_decode]传递了文件名。 1