How can I change a file's extension using PHP?

前端 未结 11 2120
清歌不尽
清歌不尽 2020-12-10 00:43

How can I change a file\'s extension using PHP?

Ex: photo.jpg to photo.exe

11条回答
  •  我在风中等你
    2020-12-10 01:30

    I needed this to change all images extensions withing a gallery to lowercase. I ended up doing the following:

    // Converts image file extensions to all lowercase
    $currentdir = opendir($gallerydir);
    while(false !== ($file = readdir($currentdir))) {
      if(strpos($file,'.JPG',1) || strpos($file,'.GIF',1) || strpos($file,'.PNG',1)) {
        $srcfile = "$gallerydir/$file";
        $filearray = explode(".",$file);
        $count = count($filearray);
        $pos = $count - 1;
        $filearray[$pos] = strtolower($filearray[$pos]);
        $file = implode(".",$filearray);
        $dstfile = "$gallerydir/$file";
        rename($srcfile,$dstfile);
      }
    }
    

    This worked for my purposes.

提交回复
热议问题