Hongmu Notes
Home Language Notes Use PHP to batch rename files in a folder, starting from number 1 while keeping the file extensions unchanged.
Language Notes PHP

Use PHP to batch rename files in a folder, starting from number 1 while keeping the file extensions unchanged.

Use PHP to batch rename files in a folder, starting from number 1 while keeping the file extensions unchanged.

function:

 隐藏内容:登录后可查看
function renameFiles($dir) {
  // 获取目录下所有文件
  $files = glob($dir . '/*');

  if (empty($files)) {
    return;
  }

  $i = 1;
  foreach ($files as $file) {
    // 获取文件的扩展名
    $ext = pathinfo($file, PATHINFO_EXTENSION);

    // 新文件名
    $newName = $dir . '/' . $i . '.' . $ext;

    // 重命名文件
    if (file_exists($newName)) {
      continue; // 如果新文件名已存在,则跳过
    }
    rename($file, $newName);

    $i++;
  }
}

This function accepts a single parameter. $dirThe directory path containing the files to be batch-renamed. The function first uses glob() The function retrieves the file paths of all files within a specified directory. Then, the function uses a loop to iterate through all files and generates a new file name for each file. The format of the new file name is: $i.jpg,among $i An increasing numerical sequence number. If the new file name already exists, the file will be skipped, and the renaming operation will not be performed.

You can call this function using the following methods:

$dir = '/path/to/files';
renameFiles($dir);

among,/path/to/files This specifies the directory path for batch renaming of files. After calling this function, it will rename all files within that directory, assigning new filenames starting from 1 and incrementing sequentially.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP ob function record

PHP ob function record Language Notes PHP

Usage of the following three functions ob_get_contents(); ob_end_clean(); ob_start(); You can use these functions to buffer local files and execute local script code. Use ob_start() to save the output code into the buffer, and the page will not be displayed; then use ob_get_contents to get the data in the buffer. o…
👁 143
PHP preg

PHP preg Language Notes PHP

The preg_match_all function is used to perform a global regular expression match. preg_match_all() Syntax int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG…
👁 169
Detailed explanation of PHP ternary operator and if

Detailed explanation of PHP ternary operator and if Language Notes PHP

Ternary operator condition ? Result 1 : Result 2 Explanation: The position in front of the question mark is the condition for judgment. If the condition is met, the result is 1, and if it is not met, the result is 2. This article compares and explains the ternary operator and if...else... in detail. I hope it will be helpful to everyone. Today when I was revising my paper online, I encountered a statement that I couldn’t understand: $if_summary = $row['IF_SUMMARY']=…
👁 191
PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232

Recommended reading

Orchard Fruit Tree Planting Garden Website Template 0240

Orchard Fruit Tree Planting Garden Website Template 0240 Practical Collection Yiyou template

An EYO CMS website template designed specifically for orchards and fruit tree plantations. Its natural and fresh design style effectively showcases the orchard environment, fruit varieties, fruit products, and the fruit-picking experience. This template helps agricultural businesses showcase their products online and expand their sales channels. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EYO CMS | EYO CMS (Eyo...
👁 55
(Adaptive mobile version) Domestic Cleaning Service PBootCMS Website Template – Download Website Source Code for Domestic Service Companies – 0336

(Adaptive mobile version) Domestic Cleaning Service PBootCMS Website Template – Download Website Source Code for Domestic Service Companies – 0336 Practical Collection pbootcms Template

This is an adaptive mobile-friendly website template designed for domestic cleaning services and household service companies, built on the PbootCMS platform. Its fresh and professional design is ideal for showcasing cleaning services, domestic service personnel, and service workflows. It helps household service companies build a strong online brand image and attract households seeking domestic assistance. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 67
Commercial Service Product Website Template 0241

Commercial Service Product Website Template 0241 Practical Collection Yiyou template

This EyouCMS template is ideal for websites specializing in commercial service products. Its professional and practical design enables effective presentation of commercial service offerings, product introductions, client testimonials, and corporate capabilities. It helps commercial service companies showcase their brand online and attract business clients. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (...
👁 41
(Adaptive mobile terminal) domestic service company pbootcms website template parenting cleaning nanny website source download 0337

(Adaptive mobile terminal) domestic service company pbootcms website template parenting cleaning nanny website source download 0337 Practical Collection pbootcms Template

An adaptive mobile phone terminal housekeeping service company and parenting cleaning nanny PbootCMS website template. The design style is warm and professional, suitable for showing the maternity sister-in-law, parenting sister-in-law, nanny and other services. It helps domestic service agencies to show their professional image online and attract new parents' families. Template display, installation instructions Website background: / admin.php account: admin password: admin decompression password: www.4s5.cn...
👁 43
Empire Toolbox V1.0

Empire Toolbox V1.0 Practical Collection empire plugin

As a webmaster who frequently uses Empire CMS, I often find certain operations to be particularly cumbersome! For example, I might want to change the Empire CMS database table prefix; or perhaps I want to batch modify the thumbnails for all articles collected from an external source; or if the images in the collected articles have become invalid, I might want to randomly replace them with new images; or I might want to batch delete other unnecessary tables from the Empire CMS database; or perhaps I've forgotten my Empire CMS admin panel password...
👁 444