This article describes how to modify a specified file suffix in php. the foreachDir function in this article can detect, read, open, and replace file directories. it is a very practical technique, if you need it, you can refer to modifying the asp suffix to php due to project requirements. I think php and Qt are both advanced languages. generally, advanced languages provide functions for adding, deleting, modifying, and querying the obtained content. After some data searches and code tests, I summarized the methods for modifying the specified file suffix in PHP and shared them with you.
Objective: to change the asp suffix in the current directory to php without affecting other "files in the suffix format", and only for the "current folder ", do not modify the files in the current folder.
The specific function code is as follows:
<? Php function foreachDir ($ dirname) {if (! Is_dir ($ dirname) {echo "{$ dirname} not valid tive dir"; exit () ;}$ handle = opendir ($ dirname ); // open the directory while ($ file = readdir ($ handle ))! = False) // read the directory {if ($ file! = "." & $ File! = '..') {If (is_dir ($ dirname. $ file) {echo $ dirname. $ file ."
"; // ForeachDir ($ dirname. $ file); // if the comment is removed, the folder file in the folder will be recursively modified} else {echo "--". $ dirname. "/". $ file."
"; $ Temp = substr ($ file, strrpos ($ file ,'. ') + 1); // Obtain the suffix format if ($ temp = "asp") {$ pos = strripos ($ file ,'. '); // get the location of the file name $ filename = substr ($ file, 0, $ pos); // Get the rename ($ dirname. '/'. $ file, $ dirname. '/'. $ filename. '. php '); // replace it with the php suffix format. }}}} ForeachDir ('../traverseMendFilename');?>
If you are interested, you can test and run the program and extend the examples in this article. I believe it will be helpful for you to learn PHP programming.
In addition, four methods for getting the file extension are provided for your reference.