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 appropriately.
Copy the code The code is as follows:
//This file and the directory to be changed Put the files in the same folder
define("STA",".gif");//The original file format
define("END",".jpg");//The 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;
}
? >
Replacing with string is not very strict. If there is a gif program in the name, it will be abnormal.
It is recommended to use the PHP function pathinfo(). The loop section can be modified as
Copy the code The code is as follows:
foreach( $arr as $t)
{
$path_parts = pathinfo($t);
if($path_parts["extension"] == STA)
{
rename($t, $path_parts["dirname"]."/".basename($t,STA).END);
}
}
http://www.bkjia.com/PHPjc/322552.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322552.htmlTechArticleI don’t deal with files very often, so I am not very familiar with traversing directories. I searched for information and modified it myself. Let everyone see if the code needs to be improved. The main purpose is to batch...