Code _php Tips for PHP to change all file suffixes under directories and subdirectories

Source: Internet
Author: User
Not very often processing files, so the traversal directory is not very familiar with, looking for information, modify their own.
Let's see if we need a perfect
The main purpose of the code is to change the file suffix in bulk because of the different types of Taobao package pictures to change the appropriate.
Copy Code code as follows:

<?php
//This file and the files under the directory to be changed are placed under the same folder
define ("STA", ". gif");// The original file format
define ("End", ". jpg");//format to change
$dir = "./";
$arr =allfile ($dir);
foreach ($arr as $t)
{
$t =str_replace (".//", "", $t);
if (Substr_count ($t, STA) >0)
{
$f 2=str_replace (STA, "", $t);
Rename ($t, $f 2. End);
}
}
//Get functions for all files under directory
function Allfile ($dir)
{
$files =array ();
if (Is_file ($dir))
{
return $dir;
}
$handle = Opendir ($dir);
if ($handle) {
while (false!== ($file = Readdir ($handle)) {
if ($file!= '. ' && $file!= ' ... ') {
$filename = $dir. "/" . $file;
if (Is_file ($filename)) {
$files [] = $filename;
}else {
$files = Array_merge ($files, Allfile ($filename));
}
}
}//End While
Closedir ($handle);
}
Return $files;
}

?>

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.