How PHP modifies the specified file suffix

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

  • Related Article

    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.