Php method for modifying the specified file suffix

Source: Internet
Author: User
Tags learn php programming
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.