重命名指定目录中的所有图像文件 [英] rename all the image files in a specified directory

查看:95
本文介绍了重命名指定目录中的所有图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建几行代码,以重命名指定目录中的所有jpg图片.父目录为 c:/xampp/htdocs/practice/haha​​/,但已重命名图像保存在 c:/xampp/htdocs/practice/

i am trying to create couple of lines of code which will rename all the jpg images in a specified directory.The parent directory was c:/xampp/htdocs/practice/haha/.But renamed images are saved in c:/xampp/htdocs/practice/

我的代码确实重命名了文件,但是问题是它从指定的目录中删除了所有图像文件,使该目录为空.它将重命名的文件存储在php根目录中,而新创建的文件不是可单击的图像文件.我想解决两个问题.

My code do rename the file ,but problem is it deletes all the image file form the specied directory leaving the directory empty.It stores the renamed file in root php directory.And the newly created files are not clickable image file.So there are two problem i want to solve.

  1. 如何在重命名之前将重命名的文件保存在相同的目录中.

2.我如何维持它们为可点击的图像文件?

2.How i can sustain them to be a clickable image file?

这是他们重命名后的样子:

this is how they looked after renaming:

$dir='c:/xampp/htdocs/practice/haha/';
echo getcwd().'</br>';
$i=1;
if(is_dir($dir)){
echo dirname($dir);
     $file=opendir($dir);

     while(($data=readdir($file))!==false){
      $info=pathinfo($data,PATHINFO_EXTENSION);
      if($info=='jpg'){
         //echo pathinfo($data,PATHINFO_BASENAME).'</br>';
         echo basename(pathinfo($data,PATHINFO_BASENAME),'.jpg').'</br>';
         rename($dir.'/'.$data,'image'.$i.'jpg');
         $i++;
      }
     }

}

推荐答案

您错过了文件名中的点:

You missed a dot in the file name:

rename($dir.'/'.$data,'image'.$i.'jpg');

这就是为什么它们不可单击的原因.改用它:

This is why they are not clickable. Use this instead:

rename($dir . '/' . $data, 'image' . $i . '.jpg');
// --------------------------------------- ^

这篇关于重命名指定目录中的所有图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆