我主要想生成一个对应的表,但是执行过程中,如果有中文的就会执行两边:最后写到文件中的数据为:
我的这个目录下的文件为:
1000.jpg
站      (文件夹)
我.txt
想生成的目标list为:
1        1000
2 站
3 我
但是结果确实:
1 1
2 站
3 我
4 2
5 3
请高手指点。下面是代码:<?php
/**
 * 此程序,主要是更改文件目录、文件名称,生成一个对应关系列表
 * 目的是去除中文的。
 */
$dirS = "C:/Program Files/Apache Group/Apache2/htdocs/FileSys/";
$dirF = "C:/Program Files/Apache Group/Apache2/htdocs/信息";
//$suffix = "mp3";

//$dirF = "D:/temp/音乐";
$arrFile = explode("/",$dirF);
$stfile = $arrFile[sizeof($arrFile)-1];
if ($handle = opendir($dirF)) {
    echo "start------------------------<br>";
    /* 这是正确地遍历目录方法 */
    $fd = fopen($dirS.$stfile.".txt",'w');
    if (!$fd) {
     echo "can not open file.";
    }
    $sourceI = 1;
    
    while (false !== ($file = readdir($handle))) {
        if ($file != '.' && $file != '..') {
         $arrName = array();
         $arrName = explode(".",$file);
         $num = sizeof($arrName);
         if (!$suffix) {
         if ($num<2) {
         $targetFile = $sourceI;
         } else {
         $targetFile = $sourceI.".".$arrName[$num-1];
         }         
         } else {
         $targetFile = $sourceI.".".$suffix;
         }
        
         rename($dirF."/".$file,$dirF."/".$targetFile);
         if (!$suffix) {
         if ($num>=2) {
         $file = str_replace(".".$arrName[$num-1],"",$file);
         }
         } else {
         $file = str_replace(".".$suffix,"",$file);
         }
        
         echo "$sourceI     $file<br>";
         fwrite($fd,$sourceI."\t".$file."\n");
         $sourceI ++;
        
        }
    }
    closedir($handle);
    fclose($fd);
}
echo "$dirF";
?>