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

标签 php linux bash

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/

相关文章:

php - 使用 PHP 将多个 MySQL 结果添加到 HTML 表中的一个单元格

php - 如何使用 PHP + shell 命令优化视频到动画 GIF 的转换

php - 对我的第一个项目 PHP 类的建议/建议

java - 从java运行linux命令,错误500,无法运行程序没有那个文件或目录

java - 为什么 Log4j2 在 Windows bash 控制台中抛出错误

php - 无法验证您的代码 32 twitter

linux - bash - 转义字符按原样获取字符串

c - epoll - 轮询多个文件描述符(即套接字)

bash - 在 VS Code 中打开一个包含内容的新文件

python - 使用python动态创建bash别名