php批量修改文件名的方法:首先打开编辑器;然后使用函数item替换函数名,代码为【if(!in_array($item,['.','..']))$arr = explode(".", $item);】。

【相关学习推荐:php图文教程】
php批量修改文件名的方法:
需求描述:
某个文件夹下有100个文件,现在需要将这个100个文件的文件名后添加字符串Abc(后缀名保持不变)。
立即学习“PHP免费学习笔记(深入)”;
代码实现:
方法一
<?php
$dir = __DIR__."image\";
$list = scandir($dir);
foreach ($list as $item) {
if(!in_array($item,['.','..'])){
$arr = explode(".", $item);
$origin_name = reset($arr);
$new_name = $origin_name.'Abc.'.end($arr);
$origin_path = $dir.$item;
$data = file_get_contents($origin_path);
$new_path = $dir.$new_name;
$res[] = file_put_contents($new_path, $data);
unlink($origin_path);
}
}方法二
<?php
$dir = __DIR__."image\";
$list = scandir($dir);
foreach ($list as $item) {
if(!in_array($item,['.','..'])){
$arr = explode(".", $item);
$origin_name = reset($arr);
$new_name = $origin_name.'Abc.'.end($arr);
$origin_path = $dir.$item;
$new_path = $dir.$new_name;
copy($origin_path, $new_path);
unlink($origin_path);
}
}方法二使用了copy函数,更加简便。
相关学习推荐:php编程(视频)
以上就是php怎样批量修改文件名的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号