Home > Backend Development > PHP Tutorial > Subdirectory php code to change the suffix of all files in the directory and subdirectories

Subdirectory php code to change the suffix of all files in the directory and subdirectories

WBOY
Release: 2016-07-29 08:43:51
Original
1081 people have browsed it

I don't deal with files very often, so I'm not very familiar with directory traversal. I searched for information and modified it myself.
Let everyone see if it needs to be improved
The main purpose of the code is to change the file suffix in batches. Since the Taobao data package image types are different, it needs to be changed to suit the needs.

Copy the code The code is as follows:


//This file and the file in the directory to be changed are placed in the same folder
define("STA",".gif"); //Original file format
define("END",".jpg"); //Format to be changed
$dir="./";
$arr=allfile($dir);
foreach($arr as $t)
{
$t=str_replace(".//","",$t);
if(substr_count($t,STA)>0)
{
$f2=str_replace(STA,"" ,$t);
rename($t,$f2.END);
}
}
//Function to get all files in the 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;
}
?>

The above introduces the subdirectory PHP code to change the directory and all file suffixes in the subdirectory, including the content of the subdirectory. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template