php更改目录及子目录下所有的文件后缀扩展名的代码_PHP
不是很经常处理文件,所以对遍历目录还不是很熟悉,找了一下资料,自己修改一下。
让大家看看是否需要完善的
代码主要的目的是 批量更改 文件后缀 由于淘宝数据包图片类型的不同 所以要改一下适合的。
复制代码 代码如下:
//本文件和要改变的目录下的文件 放在同一文件夹下
define("STA",".gif");//原来的文件格式
define("END",".jpg");//要改变的格式
$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 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;
}
?>
用字符串替换不太严谨,万一名称中就有gif程序就有异常。
建议使用PHP函数pathinfo(),循环段可修改为
复制代码 代码如下:
foreach($arr as $t)
{
$path_parts = pathinfo($t);
if($path_parts["extension"] == STA)
{
rename($t,$path_parts["dirname"]."/".basename($t,STA).END);
}
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



WebP or WebPicture is a modern image format developed by Google and has been widely used by the industry recently. These images are smaller than JPG, JPEG or PNG files, which increases page load speed and makes them valuable from an SEO perspective. But how to open WebP files in Windows 11? Given that it's a relatively new image format, first announced in March 2010, compatibility is bound to be an issue. Some photo editing software still don't support file extensions. But things change quickly! Does Windows 11 support WebP? Although WebP image files are not compatible with previous Windows 11, M

Have you ever faced any issues while transferring files that prevented you from doing so? Well, many Windows users have recently reported that they faced issues while copying and pasting files into a folder, where an error was thrown saying “The file name of the destination folder is too long”. Additionally, some other Windows users expressed frustration when opening any file and said "the file name or extension is too long" and they were unable to open the file. This disappoints users by not allowing them to transfer files to any other folder. While analyzing the issue, we have come up with a series of solutions that may help alleviate the issue and allow users to transfer files easily. If you are in a similar situation, please see this post for more information. Source: https

Having a file doesn't mean much unless you know its extension. Some files have no extension. Most of the time, the extension is probably just hidden and you can easily find it from Windows File Explorer itself using the methods listed in Section 1. Even if the file doesn't actually have an extension, it can be easily found by following the methods listed in Section 2. Finally, once you know the file extension, you can follow the steps in Section 3 to find an appropriate application to open your file. So, this article is a complete solution to all your file extension problems. Hope you enjoy reading! Section 1: How to Determine if a File Has an Extension The first step in opening a file without an extension is to make sure that the file actually does not have an extension. this

How to batch rename file suffixes in win10? Nowadays, many users are using the Win10 system, and when we usually use computers, we often use a lot of shortcut keys, because shortcut keys can make our operations more convenient, so how do we rename files in batches? Below, the editor will introduce to you the operation of batch renaming files in Win10. How to batch rename files in Win10 1. Select all the files you want to rename. 2. Right-click on a selected file and select Rename. 3. After a file is renamed, other files will be marked with serial numbers in turn. The method is very simple, but when modifying, you need to pay attention to the order of the files and the sorting position of the modified sample files. The serial number starts from the modified sample file.

How to solve the mismatch between file format and extension. During computer use, we often encounter mismatch between file format and extension. When we try to open a file, the system may prompt "The file cannot be opened" or "The file format is not supported." This situation can be confusing and frustrating, but it's actually not that difficult to fix and it only requires a few simple steps. First, we need to understand what the file format and extension mean. File format refers to the storage method and encoding method of the file. For example, a text file is

What file is the suffix bin? In computers, a file suffix is a way to identify a file type. In this question, we need to understand the meaning and purpose of bin files. Bin is the abbreviation of binary, which refers to a binary file. A binary file is a type of file that a computer can directly read and execute, in which the instructions and data of a computer program are stored in binary form. In the computer field, bin files can represent many different file types. Here are some common examples: Executable program files

The document suffix, also called the file extension, is a mechanism used by the operating system to mark file types; the main function of the document suffix is to let the system decide what software should be used to run when the user opens the specified file, so it can be used Used to protect certain files, save disk space, clear icons on the settings panel, and set associations.

Suppose you need to rename the extension of a file from one extension to another, say jpg to png. It's easy, of course! But what if you have multiple files whose extensions need to be changed? Or worse, what if these multiple files are also located in multiple folders and subfolders, within a single folder? Well, for a normal person, this can be a nightmare. But for a geek, absolutely not. The question now is, are you a geek? Well, with the help of Geek Page, you definitely are! In this article, we explain how to easily rename the extension of all files within a folder, including subfolders of your choice, from one extension to another through a batch script method. Notice:
