【发布时间】:2017-10-31 23:01:48
【问题描述】:
我在删除系统中上传文件中的括号时遇到问题。
我可以删除单个目录文件中的括号,但我很难在同一文件夹的子目录中递归地让它工作。
我正在使用 str_replace 查找括号 [ ] 并将它们替换为空白字符。
$dir = $_SERVER["DOCUMENT_ROOT"]."/uploads" ; // what directory to parse
if ( $handle = opendir ( $dir)) {
print "Directory Handle = $handles\n" ;
print "Files : \n" ;
while ( false !== ($file = readdir($handle))) {
if ( $file != "." && $file != ".." ) {
$isdir = is_dir ( $file ) ;
if ( $isdir == "1" ) {} // if its a directory do nothing
else {
$file_array[] = "$file" ; // get all the file names and put them in an array
print "$file\n" ;
} // closes else
} // closes directory check
} // closes while
} // closes opendir
//Lets go through the array
$arr_count = count( $file_array ) ; // find out how many files we have found so we can initiliase the counter
for ( $counter=1; $counter<$arr_count; $counter++ ) {
print "Array = $file_array[$counter]\n" ; // tell me how many files there are
$illegals = array("[","]");
$new = str_replace ( $illegals, "", $file_array[$counter] ) ; // now create the new file name
$ren = rename ( "$dir/$file_array[$counter]" , "$dir/$new" ) ; // now do the actual file rename
print "$new\n" ; // print out the new file name
}
closedir ( $handle ) ;
echo $dir;
【问题讨论】:
-
"我目前找不到代码,稍后会上传。"好的,那我等着……
-
抱歉没有马上上传代码。已经是凌晨 2 点了,我的女朋友在对我大喊大叫。花了一整天的时间试图找到一种方法来做到这一点。
标签: php recursion str-replace sanitize