【发布时间】:2012-03-21 20:07:17
【问题描述】:
Php 有一个方法 escapeshellcmd() 可以转义字符串中的任何字符,这些字符可能被用来欺骗 shell 命令执行任意命令。
<?php
exec(find /music -type f -iname '*mp3'", $arrSongPaths);
echo $arrSongPaths[0] //prints It Won´t Be Long.mp3;
echo escapeshellcmd($arrSongPaths[0]) //prints It Wont Be Long.mp3;
?>
有没有办法编写一个 shell 脚本来递归地重命名文件名(特别是 *mp3)并转义特殊字符?
我尝试在 php 中这样做
$escapedSongPath = escapeshellarg($arrSongPaths[0]);
exec("mv $arrSongPaths[0] $escapedSongPath");
但这没有用。无论如何,最后一行代码是不安全的,因为您正在执行一个具有潜在危险文件名 $arrSongPaths[0] 的命令。
【问题讨论】: