This article mainly introduces the method of PHP to modify the specified file suffix, the function foreachdir can realize the file directory detection, reading, opening and file suffix replacement functions, is very practical skills, the need for friends can refer to the next
Due to project requirements, the ASP suffix needs to be modified to PHP, because lazy to modify. Also think that PHP and Qt are the same high-level language, the general high-level language has to provide access to the content of the function of the increase and deletion check. After some data search and code testing, summed up the PHP modify the specified file suffix method, share to everyone.
Target: Change the ASP suffix in the current directory to PHP without affecting the other "suffix file", and only for "current folder", the folder contained in the current folder files are not modified.
The specific function code is as follows:
<?php function Foreachdir ($dirname) {if (!is_dir ($dirname)) { echo "{$dirname} not effective dir"; Exit ();} $handle =opendir ($dirname); Open the Directory while (($file = Readdir ($handle))//Read Directory { if ($file! = "." && $file! = '). { if (Is_dir ($dirname. $file)) { echo $dirname. $file. " <br/> "; Foreachdir ($dirname. $file); If the comment number is removed, the folder file in the folder will be modified recursively} else { echo "--". $dirname. /". $file." <br/> "; $temp = substr ($file, Strrpos ($file, '. ') +1); Gets the suffix format if ($temp = = "asp") { $pos = Strripos ($file, '. ');//Gets the location of the file name $filename = substr ($file, 0,$ POS); Gets the file name rename ($dirname. ' /'. $file, $dirname. ' /'. $filename. '. php '); Replace with the PHP suffix format. } } } } } foreachdir ('.. /traversemendfilename ');? >
Interested friends can test run and expand the example of this article, I believe it will be helpful to you to learn PHP programming.
Also: Attach four ways to get the file extension for your reference.