Home > php教程 > php手册 > PHP 修改文件及文件下所有文件后缀的程序代码

PHP 修改文件及文件下所有文件后缀的程序代码

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 10:40:37
Original
1233 people have browsed it

  1. //本文件和要改变的目录下的文件 放在同一文件夹下。
  2. //代码主要的目的是 批量更改 文件后缀 由于淘宝数据包图片类型的不同 所以要改一下适合的
  3. define("STA",".gif");//原来的文件格式
  4. define("END",".jpg");//要改变的格式
  5. $dir = "./";
  6. $arr = allfile($dir);
  7. foreach($arr as $t)
  8. {
  9.         $t=str_replace(".//","",$t);
  10.         if(substr_count($t,STA)>0)
  11.         {
  12.                 $f2=str_replace(STA,"",$t);
  13.                 rename($t,$f2.END);
  14.         }
  15. }
  16. //获取目录下所有文件的函数
  17. function allfile($dir)
  18. {
  19.         $files=array();
  20.         if(is_file($dir))
  21.         {
  22.                 return $dir;
  23.         }
  24.         $handle = opendir($dir);
  25.         if($handle) {
  26.                 while(false !== ($file = readdir($handle))) {
  27.                     if ($file != . && $file != ..) {
  28.                             $filename = $dir . "/" . $file;
  29.                             if(is_file($filename)) {
  30.                                     $files[] = $filename;
  31.                             }else {
  32.                                     $files = array_merge($files, allfile($filename));
  33.                             }
  34.                     }
  35.                 } // end while
  36.                 closedir($handle);
  37.         }
  38.         return $files;
  39.     }
  40. ?>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template