Home  >  Article  >  Backend Development  >  How to replace file suffix in php

How to replace file suffix in php

藏色散人
藏色散人Original
2021-06-24 10:45:143131browse

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.

How to replace file suffix in php

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!

Statement:
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