【发布时间】:2015-03-20 20:28:11
【问题描述】:
需要从文件名中删除用户请求的字符串。下面是我的功能。
$directory = $_SERVER['DOCUMENT_ROOT'].'/path/to/files/';
$strString = $objArray['frmName']; // Name to remove which comes from an array.
function doActionOnRemoveStringFromFileName($strString, $directory) {
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if(!strstr($file,$strString)) {
continue;
}
$newfilename = str_replace($strString,"",$file);
rename($directory . $file,$directory . $newfilename);
}
}
closedir($handle);
}
}
它部分工作良好。但是这个例程中的错误是,重命名操作也会影响文件的扩展名。我需要的是,只重命名文件,它不应该影响它的文件扩展名。请有任何建议。在此先感谢:)。
【问题讨论】:
标签: php image rename str-replace file-rename