Home >Backend Development >PHP Problem >How to replace file suffix in php
php method to replace file suffix: first create a PHP sample file; then replace the file suffix through the "function changeTypeName($path, $oldTypaName, $newTypeName){...}" method.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How to replace the file suffix with php?
php Change the suffix name of the file in the file directory
changeTypeName("E:/bak/video/code/views", "html", "phtml"); //改变文件目录下文件的后缀名 function changeTypeName($path, $oldTypaName, $newTypeName) { $path = "glob://$path/*"; $files = new DirectoryIterator($path); foreach ($files as $file) { $fileRealPath = $file->getRealPath(); //文件绝对路径 $compressCssFileRealPath = str_replace(".$oldTypaName", ".$newTypeName", $fileRealPath); rename($fileRealPath, $compressCssFileRealPath); } }
Related introduction:
rename() function renames the file or directory.
If successful, the function returns true. On failure, returns false.
Syntax
rename(oldname,newname,context)
Parameters
oldname Required. Specifies the file or directory to be renamed.
newname Required. Specifies the new name of the file or directory.
context Optional. Specifies the environment for a file handle. context is a set of options that modify the behavior of the stream.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to replace file suffix in php. For more information, please follow other related articles on the PHP Chinese website!