gpt4 book ai didi

php - 递归重命名文件名并转义特殊字符的 Shell 脚本?

转载 作者:太空宇宙 更新时间:2023-11-04 09:52:05 24 4
gpt4 key购买 nike

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] 的命令。

最佳答案

出于对所有安全相关事物的热爱,您为什么不使用 php rename 命令 - 它不会遇到任何 shell 转义问题。将 exec("mv ...") 替换为:

rename($arrSongPaths[0], $escapedSongPath)

...并检查错误。

而不是使用 exec(find...) 使用来自 globrecursive_glob 提示php运行页面。

关于php - 递归重命名文件名并转义特殊字符的 Shell 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9812040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com