Home > Backend Development > PHP Tutorial > Introduction to how to batch rename all files in a folder with PHP

Introduction to how to batch rename all files in a folder with PHP

巴扎黑
Release: 2023-03-15 19:56:02
Original
1846 people have browsed it

This article mainly introduces PHP's method of batch renaming all files in a folder, involving PHP's related operation skills such as traversing files under the folder, string search, interception, and renaming files with the rename function. Friends who need it can refer to

This article describes the method of batch renaming all files in a folder in PHP. I share it with you for your reference. The details are as follows:

It’s tiring to manually rename one by one like this. So let’s be lazy.

My renaming rule is to replace all spaces with "_", and then add an "_s" after it.


<?php
$paths = "C://Documents and Settings//sk//Desktop//s//";
$d = dir($paths);
while (false !== ($entry = $d->read())) {
  $table_change = array(&#39; &#39;=>&#39;_&#39;);
  $newName = strtr($entry,$table_change);
  $newName = substr($newName, 0,-4);
  rename($paths.$entry, $paths.$newName."_s.jpg");
}
$d->close();
echo "done";
?>
Copy after login

The above is the detailed content of Introduction to how to batch rename all files in a folder with PHP. For more information, please follow other related articles on the PHP Chinese website!

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