How to modify the suffix of a specified file in php

巴扎黑
Release: 2023-03-01 09:22:02
Original
2917 people have browsed it

This article mainly introduces the method of modifying the specified file suffix in PHP. The function foreachDir in the article can realize the functions of detecting, reading, opening and file suffix replacement of file directories. It is a very practical skill. Friends who need it can refer to it

Due to project requirements, I need to change the asp suffix to php, but I am too lazy to change it one by one. I also feel that php, like Qt, is a high-level language. Generally, high-level languages provide functions for adding, deleting, modifying and checking the obtained content. After some information search and code testing, I summarized the method of modifying the specified file suffix in PHP and shared it with everyone.

Goal: Change the asp suffix in the current directory to php without affecting other "files in suffix format", and it is only for the "current folder" and does not modify the files in the folders contained in the current folder.

The specific function code is as follows:

"; //foreachDir($dirname.$file); //如果注释号去掉,将会递归修改文件夹内的文件夹文件 } else { echo "--".$dirname."/".$file."
"; $temp = substr($file, strrpos($file, '.')+1); //获取后缀格式 if ($temp == "asp") { $pos = strripos($file,'.'); //获取到文件名的位置 $filename = substr($file,0,$pos); //获取文件名 rename($dirname.'/'.$file,$dirname.'/'.$filename.'.php'); //替换为php后缀格式。 } } } } } foreachDir('../traverseMendFilename'); ?>
Copy after login

Interested friends can test run and expand and improve the examples in this article. I believe it will be helpful for everyone to learn PHP programming.

Also: Attached are four methods for obtaining file extensions for your reference.

Related labels:
source:php.cn
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 Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!