douzou0073 2010-10-03 22:21
浏览 20
已采纳

php重命名表现得很奇怪?

i have this script which i will post absolutely unmodified:

<?
chdir("data");
$files = glob("*");
shuffle($files);
var_dump($files);
$i=0;
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
foreach($files as $file) {
  $i++;
  $k = $i;
  $mime = finfo_file($finfo, $file);
  if(strpos($mime,"gif") !== false) {
    $ext = "gif";
  } else {
    $ext = "jpg";
  }
  if($k < 10) {
    $k = "00".$k;
  } else if($k < 100) {
    $k = "0".$k;
  }
  $k = $k.".".$ext;
  rename($file,$k);
  echo $k."
";
}

the folder data has some image files (jpg and gif) in it. but when i run it, suddenly a lot of images are just gone! 2/3rd of the images just got deleted... i don't understand how? i have an ext3 filesystem and PHP 5.3.2

  • 写回答

2条回答 默认 最新

  • dongmuyan5638 2010-10-03 23:38
    关注

    I can't see anything in the code that would definately cause this behaviour. The most likely cause I could think of is perhaps rename($file,$k); is overwriting files that already exist. You could add the following to rule this out:

    if(file_exists($k.".".$ext)) {
        $k .= ".0" ;
    }
    while(file_exists($k.".".$ext)) {
        $k .= "0" ;
    }
    $k = $k.".".$ext;
    rename($file,$k);
    

    The other thought I had is that perhaps something is going wrong with the chdir("data") which you could check by inserting the full path before $file and $k when calling the rename. I don't think this is very likely though.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效