Home Backend Development PHP Tutorial php更改目录及子目录下所有的文件后缀扩展名的代码_PHP

php更改目录及子目录下所有的文件后缀扩展名的代码_PHP

Jun 01, 2016 pm 12:17 PM
extension name File extension

不是很经常处理文件,所以对遍历目录还不是很熟悉,找了一下资料,自己修改一下。
让大家看看是否需要完善的
代码主要的目的是 批量更改 文件后缀 由于淘宝数据包图片类型的不同 所以要改一下适合的。
复制代码 代码如下:
//本文件和要改变的目录下的文件 放在同一文件夹下
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);
  }
}

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

4 Quick Ways to Open Webp Images on Windows 11 4 Quick Ways to Open Webp Images on Windows 11 Sep 30, 2023 pm 02:13 PM

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

How to fix file names or extensions that are too long in Windows 11? How to fix file names or extensions that are too long in Windows 11? Apr 22, 2023 pm 04:37 PM

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

How to open files without extension in Windows 11, 10 How to open files without extension in Windows 11, 10 Apr 14, 2023 pm 05:52 PM

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 How to batch rename file suffixes in win10 Jul 06, 2023 pm 07:37 PM

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 deal with inconsistent file formats and extensions How to deal with inconsistent file formats and extensions Feb 19, 2024 pm 04:47 PM

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? What file is the suffix bin? Feb 25, 2024 pm 08:45 PM

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

What is the document suffix? What is the document suffix? Mar 01, 2023 pm 03:06 PM

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.

How to rename the extension of all files within a folder, including subfolders How to rename the extension of all files within a folder, including subfolders Apr 14, 2023 pm 12:22 PM

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:

See all articles